diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b88c97b8e988d..2f99785674a1a 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: """ @@ -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 , should be the first line. val = buf.getvalue().replace("<", r"<", 1) @@ -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 " @@ -1058,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",