From 89610c6d68b0e640e97261982f6462126ed6840c Mon Sep 17 00:00:00 2001 From: tp Date: Sat, 13 Nov 2021 18:22:37 +0000 Subject: [PATCH 1/5] TYP: improve typing for type DataFrame.to_string --- pandas/core/frame.py | 56 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b88c97b8e988d..aeee1cffdfcb4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -989,15 +989,13 @@ def __repr__(self) -> str: """ Return a string representation for a particular DataFrame. """ - buf = StringIO("") if self._info_repr(): + buf = StringIO("") self.info(buf=buf) return buf.getvalue() repr_params = fmt.get_dataframe_repr_params() - self.to_string(buf=buf, **repr_params) - - return buf.getvalue() + return self.to_string(**repr_params) def _repr_html_(self) -> str | None: """ @@ -1043,6 +1041,56 @@ def _repr_html_(self) -> str | None: else: return None + @overload + def to_string( + self, + buf: None = None, + columns: Sequence[str] | None = None, + col_space: int | None = None, + header: bool | Sequence[str] = True, + index: bool = True, + na_rep: str = "NaN", + formatters: fmt.FormattersType | None = None, + float_format: fmt.FloatFormatType | None = None, + sparsify: bool | None = None, + index_names: bool = True, + justify: str | None = None, + max_rows: int | None = None, + min_rows: int | None = None, + max_cols: int | None = None, + show_dimensions: bool = False, + decimal: str = ".", + line_width: int | None = None, + max_colwidth: int | None = None, + encoding: str | None = None, + ) -> str: + ... + + @overload + def to_string( + self, + buf: FilePathOrBuffer[str], + columns: Sequence[str] | None = None, + col_space: int | None = None, + header: bool | Sequence[str] = True, + index: bool = True, + na_rep: str = "NaN", + formatters: fmt.FormattersType | None = None, + float_format: fmt.FloatFormatType | None = None, + sparsify: bool | None = None, + index_names: bool = True, + justify: str | None = None, + max_rows: int | None = None, + min_rows: int | None = None, + max_cols: int | None = None, + show_dimensions: bool = False, + decimal: str = ".", + line_width: int | None = None, + max_colwidth: int | None = None, + encoding: str | None = None, + ) -> None: + ... + @Substitution( header_type="bool or sequence of strings", header="Write out the column names. If a list of strings " From 2345c7da7e6e9ecefa132d9cdf534313eff0ff80 Mon Sep 17 00:00:00 2001 From: tp Date: Sat, 13 Nov 2021 18:41:06 +0000 Subject: [PATCH 2/5] small cleanups --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index aeee1cffdfcb4..d9a9929bcee76 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -990,7 +990,7 @@ def __repr__(self) -> str: Return a string representation for a particular DataFrame. """ if self._info_repr(): - buf = StringIO("") + buf = StringIO() self.info(buf=buf) return buf.getvalue() @@ -1004,7 +1004,7 @@ def _repr_html_(self) -> str | None: Mainly for IPython notebook. """ if self._info_repr(): - buf = StringIO("") + buf = StringIO() self.info(buf=buf) # need to escape the , should be the first line. val = buf.getvalue().replace("<", r"<", 1) From 0debdb67970b83d049c73f21be447b162303c337 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 15 Nov 2021 19:38:48 +0000 Subject: [PATCH 3/5] ellipsis as default --- pandas/core/frame.py | 74 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d9a9929bcee76..c640c6bcc741a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1044,25 +1044,25 @@ def _repr_html_(self) -> str | None: @overload def to_string( self, - buf: None = None, - columns: Sequence[str] | None = None, - col_space: int | None = None, - header: bool | Sequence[str] = True, - index: bool = True, - na_rep: str = "NaN", - formatters: fmt.FormattersType | None = None, - float_format: fmt.FloatFormatType | None = None, - sparsify: bool | None = None, - index_names: bool = True, - justify: str | None = None, - max_rows: int | None = None, - min_rows: int | None = None, - max_cols: int | None = None, - show_dimensions: bool = False, - decimal: str = ".", - line_width: int | None = None, - max_colwidth: int | None = None, - encoding: str | None = None, + buf: None = ..., + columns: Sequence[str] | None = ..., + col_space: int | None = ..., + header: bool | Sequence[str] = ..., + index: bool = ..., + na_rep: str = ..., + formatters: fmt.FormattersType | None = ..., + float_format: fmt.FloatFormatType | None = ..., + sparsify: bool | None = ..., + index_names: bool = ..., + justify: str | None = ..., + max_rows: int | None = ..., + max_cols: int | None = ..., + show_dimensions: bool = ..., + decimal: str = ..., + line_width: int | None = ..., + min_rows: int | None = ..., + max_colwidth: int | None = ..., + encoding: str | None = ..., ) -> str: ... @@ -1070,24 +1070,24 @@ def to_string( def to_string( self, buf: FilePathOrBuffer[str], - columns: Sequence[str] | None = None, - col_space: int | None = None, - header: bool | Sequence[str] = True, - index: bool = True, - na_rep: str = "NaN", - formatters: fmt.FormattersType | None = None, - float_format: fmt.FloatFormatType | None = None, - sparsify: bool | None = None, - index_names: bool = True, - justify: str | None = None, - max_rows: int | None = None, - min_rows: int | None = None, - max_cols: int | None = None, - show_dimensions: bool = False, - decimal: str = ".", - line_width: int | None = None, - max_colwidth: int | None = None, - encoding: str | None = None, + columns: Sequence[str] | None = ..., + col_space: int | None = ..., + header: bool | Sequence[str] = ..., + index: bool = ..., + na_rep: str = ..., + formatters: fmt.FormattersType | None = ..., + float_format: fmt.FloatFormatType | None = ..., + sparsify: bool | None = ..., + index_names: bool = ..., + justify: str | None = ..., + max_rows: int | None = ..., + max_cols: int | None = ..., + show_dimensions: bool = ..., + decimal: str = ..., + line_width: int | None = ..., + min_rows: int | None = ..., + max_colwidth: int | None = ..., + encoding: str | None = ..., ) -> None: ... From a483e9c97193b3fc726aa7f1d5dca7a8a65f881f Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 16 Nov 2021 06:49:10 +0000 Subject: [PATCH 4/5] correct typing for col_space --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c640c6bcc741a..133197ca213a7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1106,7 +1106,7 @@ def to_string( self, buf: FilePathOrBuffer[str] | None = None, columns: Sequence[str] | None = None, - col_space: int | None = None, + col_space: int | list[int] | dict[Hashable, int] | None = None, header: bool | Sequence[str] = True, index: bool = True, na_rep: str = "NaN", From 9dc078f9352bcfa607e7beec5b31533aca09fb74 Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 16 Nov 2021 07:25:20 +0000 Subject: [PATCH 5/5] correct typing for col_space, II --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 133197ca213a7..2f99785674a1a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1046,7 +1046,7 @@ def to_string( self, buf: None = ..., columns: Sequence[str] | None = ..., - col_space: int | None = ..., + col_space: int | list[int] | dict[Hashable, int] | None = ..., header: bool | Sequence[str] = ..., index: bool = ..., na_rep: str = ..., @@ -1071,7 +1071,7 @@ def to_string( self, buf: FilePathOrBuffer[str], columns: Sequence[str] | None = ..., - col_space: int | None = ..., + col_space: int | list[int] | dict[Hashable, int] | None = ..., header: bool | Sequence[str] = ..., index: bool = ..., na_rep: str = ...,