Skip to content

Commit fb567c8

Browse files
authored
TYP: improve typing for DataFrame.to_string (#44426)
1 parent 6240b1f commit fb567c8

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

pandas/core/frame.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -989,15 +989,13 @@ def __repr__(self) -> str:
989989
"""
990990
Return a string representation for a particular DataFrame.
991991
"""
992-
buf = StringIO("")
993992
if self._info_repr():
993+
buf = StringIO()
994994
self.info(buf=buf)
995995
return buf.getvalue()
996996

997997
repr_params = fmt.get_dataframe_repr_params()
998-
self.to_string(buf=buf, **repr_params)
999-
1000-
return buf.getvalue()
998+
return self.to_string(**repr_params)
1001999

10021000
def _repr_html_(self) -> str | None:
10031001
"""
@@ -1006,7 +1004,7 @@ def _repr_html_(self) -> str | None:
10061004
Mainly for IPython notebook.
10071005
"""
10081006
if self._info_repr():
1009-
buf = StringIO("")
1007+
buf = StringIO()
10101008
self.info(buf=buf)
10111009
# need to escape the <class>, should be the first line.
10121010
val = buf.getvalue().replace("<", r"&lt;", 1)
@@ -1043,6 +1041,56 @@ def _repr_html_(self) -> str | None:
10431041
else:
10441042
return None
10451043

1044+
@overload
1045+
def to_string(
1046+
self,
1047+
buf: None = ...,
1048+
columns: Sequence[str] | None = ...,
1049+
col_space: int | list[int] | dict[Hashable, int] | None = ...,
1050+
header: bool | Sequence[str] = ...,
1051+
index: bool = ...,
1052+
na_rep: str = ...,
1053+
formatters: fmt.FormattersType | None = ...,
1054+
float_format: fmt.FloatFormatType | None = ...,
1055+
sparsify: bool | None = ...,
1056+
index_names: bool = ...,
1057+
justify: str | None = ...,
1058+
max_rows: int | None = ...,
1059+
max_cols: int | None = ...,
1060+
show_dimensions: bool = ...,
1061+
decimal: str = ...,
1062+
line_width: int | None = ...,
1063+
min_rows: int | None = ...,
1064+
max_colwidth: int | None = ...,
1065+
encoding: str | None = ...,
1066+
) -> str:
1067+
...
1068+
1069+
@overload
1070+
def to_string(
1071+
self,
1072+
buf: FilePathOrBuffer[str],
1073+
columns: Sequence[str] | None = ...,
1074+
col_space: int | list[int] | dict[Hashable, int] | None = ...,
1075+
header: bool | Sequence[str] = ...,
1076+
index: bool = ...,
1077+
na_rep: str = ...,
1078+
formatters: fmt.FormattersType | None = ...,
1079+
float_format: fmt.FloatFormatType | None = ...,
1080+
sparsify: bool | None = ...,
1081+
index_names: bool = ...,
1082+
justify: str | None = ...,
1083+
max_rows: int | None = ...,
1084+
max_cols: int | None = ...,
1085+
show_dimensions: bool = ...,
1086+
decimal: str = ...,
1087+
line_width: int | None = ...,
1088+
min_rows: int | None = ...,
1089+
max_colwidth: int | None = ...,
1090+
encoding: str | None = ...,
1091+
) -> None:
1092+
...
1093+
10461094
@Substitution(
10471095
header_type="bool or sequence of strings",
10481096
header="Write out the column names. If a list of strings "
@@ -1058,7 +1106,7 @@ def to_string(
10581106
self,
10591107
buf: FilePath | WriteBuffer[str] | None = None,
10601108
columns: Sequence[str] | None = None,
1061-
col_space: int | None = None,
1109+
col_space: int | list[int] | dict[Hashable, int] | None = None,
10621110
header: bool | Sequence[str] = True,
10631111
index: bool = True,
10641112
na_rep: str = "NaN",

0 commit comments

Comments
 (0)