Skip to content

Commit

Permalink
TYP: improve typing for DataFrame.to_string (pandas-dev#44426)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored Nov 18, 2021
1 parent 6240b1f commit fb567c8
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -1006,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 <class>, should be the first line.
val = buf.getvalue().replace("<", r"&lt;", 1)
Expand Down Expand Up @@ -1043,6 +1041,56 @@ def _repr_html_(self) -> str | None:
else:
return None

@overload
def to_string(
self,
buf: None = ...,
columns: Sequence[str] | None = ...,
col_space: int | list[int] | dict[Hashable, 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:
...

@overload
def to_string(
self,
buf: FilePathOrBuffer[str],
columns: Sequence[str] | None = ...,
col_space: int | list[int] | dict[Hashable, 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:
...

@Substitution(
header_type="bool or sequence of strings",
header="Write out the column names. If a list of strings "
Expand All @@ -1058,7 +1106,7 @@ def to_string(
self,
buf: FilePath | WriteBuffer[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",
Expand Down

0 comments on commit fb567c8

Please sign in to comment.