From e7a97e7a9c09cf6a074451db5889099e63a396bc Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 11 Oct 2021 23:31:45 +0100 Subject: [PATCH 1/4] REF: extract params used in DataFrame.__repr__ --- pandas/core/frame.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 76a00071c8adc..5e9d2d98a97df 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -994,26 +994,24 @@ 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") + repr_params = self._get_repr_params() + self.to_string(buf=buf, **repr_params) + + return buf.getvalue() + + def _get_repr_params(self) -> dict[str, Any]: 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, - ) - - return buf.getvalue() + 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"), + "width": width, + } def _repr_html_(self) -> str | None: """ From c907a4681231dc0e6d673a1d233b11ce93f97ee8 Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 12 Oct 2021 00:18:56 +0100 Subject: [PATCH 2/4] fix param name --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5e9d2d98a97df..e118a82afdd30 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1001,16 +1001,16 @@ def __repr__(self) -> str: def _get_repr_params(self) -> dict[str, Any]: if get_option("display.expand_frame_repr"): - width, _ = console.get_console_size() + line_width, _ = console.get_console_size() else: - width = None + 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"), - "width": width, + "line_width": line_width, } def _repr_html_(self) -> str | None: From 77c551349667a571240009918ace7cd2b6792140 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 14 Oct 2021 16:41:10 +0100 Subject: [PATCH 3/4] move _get_repr_params -> fmt.get_dataframe_repr_params --- pandas/core/frame.py | 16 +--------------- pandas/io/formats/format.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e118a82afdd30..9ce8a19b98857 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -994,25 +994,11 @@ def __repr__(self) -> str: self.info(buf=buf) return buf.getvalue() - repr_params = self._get_repr_params() + repr_params = fmt.get_dataframe_repr_params() self.to_string(buf=buf, **repr_params) return buf.getvalue() - def _get_repr_params(self) -> dict[str, Any]: - 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, - } - def _repr_html_(self) -> str | None: """ Return a html representation for a particular DataFrame. diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index cfda2911db73f..4ea37b3081688 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() + >>> assert 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.""" From e34f3ba7193ab67fb58ef8e5610602ae1acef70c Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 14 Oct 2021 18:07:48 +0100 Subject: [PATCH 4/4] fix doc string example --- pandas/io/formats/format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 4ea37b3081688..07811be909330 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -495,7 +495,7 @@ def get_dataframe_repr_params() -> dict[str, Any]: >>> >>> df = pd.DataFrame([[1, 2], [3, 4]]) >>> repr_params = pd.io.formats.format.get_dataframe_repr_params() - >>> assert repr(df) == df.to_string(**repr_params) + >>> repr(df) == df.to_string(**repr_params) True """ from pandas.io.formats import console