From 4c1b9b8de98dcf504161a5e15a7cc0e90ea8baa9 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 16 Sep 2019 14:36:36 +0530 Subject: [PATCH 1/5] annotate all rendering methods --- pandas/core/frame.py | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 44d3d840016fe..6e74ffae38d77 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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. @@ -716,24 +716,24 @@ def _repr_html_(self): 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, - ): + columns: Optional[List[str]] = None, + col_space: Optional[Union[str, int]] = None, + header: Union[bool, List[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 From 69cc6f423e1c89f5215134c987e189032b74d9d6 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 16 Sep 2019 17:13:15 +0530 Subject: [PATCH 2/5] add type for buf and fix type for col_space --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6e74ffae38d77..66168376044b5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 @@ -715,9 +715,9 @@ def _repr_html_(self) -> Optional[str]: @Substitution(shared_params=fmt.common_docstring, returns=fmt.return_docstring) def to_string( self, - buf=None, + buf: Optional[FilePathOrBuffer[str]] = None, columns: Optional[List[str]] = None, - col_space: Optional[Union[str, int]] = None, + col_space: Optional[int] = None, header: Union[bool, List[str]] = True, index: bool = True, na_rep: str = "NaN", From b102b593902d9a71df24910ccecf762e088ee7cc Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Tue, 17 Sep 2019 15:08:58 +0530 Subject: [PATCH 3/5] use Sequence for columns and header of DataFrameFormatter --- pandas/core/frame.py | 6 +++--- pandas/io/formats/format.py | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 66168376044b5..b92fd44b8a804 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 @@ -716,9 +716,9 @@ def _repr_html_(self) -> Optional[str]: def to_string( self, buf: Optional[FilePathOrBuffer[str]] = None, - columns: Optional[List[str]] = None, + columns: Optional[Sequence[str]] = None, col_space: Optional[int] = None, - header: Union[bool, List[str]] = True, + header: Union[bool, Sequence[str]] = True, index: bool = True, na_rep: str = "NaN", formatters: Optional[fmt.formatters_type] = None, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 4a66ad48d1318..89d18be585951 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -21,6 +21,7 @@ Iterable, List, Optional, + Sequence, Tuple, Type, Union, @@ -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, From b082a89b966ea035f7633c0efbfbb006dfe0a30f Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Tue, 17 Sep 2019 15:12:14 +0530 Subject: [PATCH 4/5] update docstring --- pandas/io/formats/format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 89d18be585951..6d27bf5c08393 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -91,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 : bool or sequence, optional %(header)s. index : bool, optional, default True Whether to print index (row) labels. From b13f9363d5458a168f884ad7c92734b9b1ad6d18 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Wed, 18 Sep 2019 11:44:18 +0530 Subject: [PATCH 5/5] fix docstring for param header of to_string and to_html --- pandas/core/frame.py | 2 ++ pandas/io/formats/format.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b92fd44b8a804..b7961b28f0252 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -706,6 +706,7 @@ def _repr_html_(self) -> Optional[str]: 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", @@ -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 6d27bf5c08393..e7a7b34100a23 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -91,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 or sequence, optional + header : %(header_type)s, optional %(header)s. index : bool, optional, default True Whether to print index (row) labels.