diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 44d3d840016fe..b7961b28f0252 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -15,7 +15,7 @@ import itertools import sys from textwrap import dedent -from typing import FrozenSet, List, Optional, Set, Tuple, Type, Union +from typing import FrozenSet, List, Optional, Sequence, Set, Tuple, Type, Union import warnings import numpy as np @@ -80,7 +80,7 @@ ) from pandas.core.dtypes.missing import isna, notna -from pandas._typing import Axes, Dtype +from pandas._typing import Axes, Dtype, FilePathOrBuffer from pandas.core import algorithms, common as com, nanops, ops from pandas.core.accessor import CachedAccessor from pandas.core.arrays import Categorical, ExtensionArray @@ -559,14 +559,14 @@ def _is_homogeneous_type(self) -> bool: # ---------------------------------------------------------------------- # Rendering Methods - def _repr_fits_vertical_(self): + def _repr_fits_vertical_(self) -> bool: """ Check length against max_rows. """ max_rows = get_option("display.max_rows") return len(self) <= max_rows - def _repr_fits_horizontal_(self, ignore_width=False): + def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool: """ Check if full repr fits in horizontal boundaries imposed by the display options width and max_columns. @@ -620,7 +620,7 @@ def _repr_fits_horizontal_(self, ignore_width=False): return repr_width < width - def _info_repr(self): + def _info_repr(self) -> bool: """ True if the repr should show the info view. """ @@ -629,7 +629,7 @@ def _info_repr(self): self._repr_fits_horizontal_() and self._repr_fits_vertical_() ) - def __repr__(self): + def __repr__(self) -> str: """ Return a string representation for a particular DataFrame. """ @@ -659,7 +659,7 @@ def __repr__(self): return buf.getvalue() - def _repr_html_(self): + def _repr_html_(self) -> Optional[str]: """ Return a html representation for a particular DataFrame. @@ -706,6 +706,7 @@ def _repr_html_(self): return None @Substitution( + header_type="bool or sequence", header="Write out the column names. If a list of strings " "is given, it is assumed to be aliases for the " "column names", @@ -715,25 +716,25 @@ def _repr_html_(self): @Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring) def to_string( self, - buf=None, - columns=None, - col_space=None, - header=True, - index=True, - na_rep="NaN", - formatters=None, - float_format=None, - sparsify=None, - index_names=True, - justify=None, - max_rows=None, - min_rows=None, - max_cols=None, - show_dimensions=False, - decimal=".", - line_width=None, - max_colwidth=None, - ): + buf: Optional[FilePathOrBuffer[str]] = None, + columns: Optional[Sequence[str]] = None, + col_space: Optional[int] = None, + header: Union[bool, Sequence[str]] = True, + index: bool = True, + na_rep: str = "NaN", + formatters: Optional[fmt.formatters_type] = None, + float_format: Optional[fmt.float_format_type] = None, + sparsify: Optional[bool] = None, + index_names: bool = True, + justify: Optional[str] = None, + max_rows: Optional[int] = None, + min_rows: Optional[int] = None, + max_cols: Optional[int] = None, + show_dimensions: bool = False, + decimal: str = ".", + line_width: Optional[int] = None, + max_colwidth: Optional[int] = None, + ) -> Optional[str]: """ Render a DataFrame to a console-friendly tabular output. %(shared_params)s @@ -2234,6 +2235,7 @@ def to_parquet( ) @Substitution( + header_type="bool", header="Whether to print column labels, default True", col_space_type="str or int", col_space="The minimum width of each column in CSS length " diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 4a66ad48d1318..e7a7b34100a23 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -21,6 +21,7 @@ Iterable, List, Optional, + Sequence, Tuple, Type, Union, @@ -90,7 +91,7 @@ The subset of columns to write. Writes all columns by default. col_space : %(col_space_type)s, optional %(col_space)s. - header : bool, optional + header : %(header_type)s, optional %(header)s. index : bool, optional, default True Whether to print index (row) labels. @@ -530,9 +531,9 @@ class DataFrameFormatter(TableFormatter): def __init__( self, frame: "DataFrame", - columns: Optional[List[str]] = None, + columns: Optional[Sequence[str]] = None, col_space: Optional[Union[str, int]] = None, - header: Union[bool, List[str]] = True, + header: Union[bool, Sequence[str]] = True, index: bool = True, na_rep: str = "NaN", formatters: Optional[formatters_type] = None,