diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 76a00071c8adc..9ce8a19b98857 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -994,24 +994,8 @@ def __repr__(self) -> str: self.info(buf=buf) return buf.getvalue() - max_rows = get_option("display.max_rows") - min_rows = get_option("display.min_rows") - max_cols = get_option("display.max_columns") - max_colwidth = get_option("display.max_colwidth") - show_dimensions = get_option("display.show_dimensions") - if get_option("display.expand_frame_repr"): - width, _ = console.get_console_size() - else: - width = None - self.to_string( - buf=buf, - max_rows=max_rows, - min_rows=min_rows, - max_cols=max_cols, - line_width=width, - max_colwidth=max_colwidth, - show_dimensions=show_dimensions, - ) + repr_params = fmt.get_dataframe_repr_params() + self.to_string(buf=buf, **repr_params) return buf.getvalue() diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index cfda2911db73f..07811be909330 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -483,6 +483,37 @@ def get_adjustment() -> TextAdjustment: return TextAdjustment() +def get_dataframe_repr_params() -> dict[str, Any]: + """Get the parameters used to repr(dataFrame) calls using DataFrame.to_string. + + Supplying these parameters to DataFrame.to_string is equivalent to calling + ``repr(DataFrame)``. This is useful if you want to adjust the repr output. + + Example + ------- + >>> import pandas as pd + >>> + >>> df = pd.DataFrame([[1, 2], [3, 4]]) + >>> repr_params = pd.io.formats.format.get_dataframe_repr_params() + >>> repr(df) == df.to_string(**repr_params) + True + """ + from pandas.io.formats import console + + if get_option("display.expand_frame_repr"): + line_width, _ = console.get_console_size() + else: + line_width = None + return { + "max_rows": get_option("display.max_rows"), + "min_rows": get_option("display.min_rows"), + "max_cols": get_option("display.max_columns"), + "max_colwidth": get_option("display.max_colwidth"), + "show_dimensions": get_option("display.show_dimensions"), + "line_width": line_width, + } + + class DataFrameFormatter: """Class for processing dataframe formatting options and data."""