Skip to content

Commit 75be36e

Browse files
authored
REF: extract params used in DataFrame.__repr__ (#43987)
1 parent 0432bf2 commit 75be36e

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

pandas/core/frame.py

+2-18
Original file line numberDiff line numberDiff line change
@@ -994,24 +994,8 @@ def __repr__(self) -> str:
994994
self.info(buf=buf)
995995
return buf.getvalue()
996996

997-
max_rows = get_option("display.max_rows")
998-
min_rows = get_option("display.min_rows")
999-
max_cols = get_option("display.max_columns")
1000-
max_colwidth = get_option("display.max_colwidth")
1001-
show_dimensions = get_option("display.show_dimensions")
1002-
if get_option("display.expand_frame_repr"):
1003-
width, _ = console.get_console_size()
1004-
else:
1005-
width = None
1006-
self.to_string(
1007-
buf=buf,
1008-
max_rows=max_rows,
1009-
min_rows=min_rows,
1010-
max_cols=max_cols,
1011-
line_width=width,
1012-
max_colwidth=max_colwidth,
1013-
show_dimensions=show_dimensions,
1014-
)
997+
repr_params = fmt.get_dataframe_repr_params()
998+
self.to_string(buf=buf, **repr_params)
1015999

10161000
return buf.getvalue()
10171001

pandas/io/formats/format.py

+31
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,37 @@ def get_adjustment() -> TextAdjustment:
483483
return TextAdjustment()
484484

485485

486+
def get_dataframe_repr_params() -> dict[str, Any]:
487+
"""Get the parameters used to repr(dataFrame) calls using DataFrame.to_string.
488+
489+
Supplying these parameters to DataFrame.to_string is equivalent to calling
490+
``repr(DataFrame)``. This is useful if you want to adjust the repr output.
491+
492+
Example
493+
-------
494+
>>> import pandas as pd
495+
>>>
496+
>>> df = pd.DataFrame([[1, 2], [3, 4]])
497+
>>> repr_params = pd.io.formats.format.get_dataframe_repr_params()
498+
>>> repr(df) == df.to_string(**repr_params)
499+
True
500+
"""
501+
from pandas.io.formats import console
502+
503+
if get_option("display.expand_frame_repr"):
504+
line_width, _ = console.get_console_size()
505+
else:
506+
line_width = None
507+
return {
508+
"max_rows": get_option("display.max_rows"),
509+
"min_rows": get_option("display.min_rows"),
510+
"max_cols": get_option("display.max_columns"),
511+
"max_colwidth": get_option("display.max_colwidth"),
512+
"show_dimensions": get_option("display.show_dimensions"),
513+
"line_width": line_width,
514+
}
515+
516+
486517
class DataFrameFormatter:
487518
"""Class for processing dataframe formatting options and data."""
488519

0 commit comments

Comments
 (0)