Skip to content

Commit c952eb2

Browse files
authored
REF: extract params used in Series.__repr__ (#44218)
1 parent df8ac2f commit c952eb2

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

pandas/core/series.py

+2-25
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"""
44
from __future__ import annotations
55

6-
from io import StringIO
7-
from shutil import get_terminal_size
86
from textwrap import dedent
97
from typing import (
108
IO,
@@ -1460,29 +1458,8 @@ def __repr__(self) -> str:
14601458
"""
14611459
Return a string representation for a particular Series.
14621460
"""
1463-
buf = StringIO("")
1464-
width, height = get_terminal_size()
1465-
max_rows = (
1466-
height
1467-
if get_option("display.max_rows") == 0
1468-
else get_option("display.max_rows")
1469-
)
1470-
min_rows = (
1471-
height
1472-
if get_option("display.max_rows") == 0
1473-
else get_option("display.min_rows")
1474-
)
1475-
show_dimensions = get_option("display.show_dimensions")
1476-
1477-
self.to_string(
1478-
buf=buf,
1479-
name=self.name,
1480-
dtype=self.dtype,
1481-
min_rows=min_rows,
1482-
max_rows=max_rows,
1483-
length=show_dimensions,
1484-
)
1485-
return buf.getvalue()
1461+
repr_params = fmt.get_series_repr_params(self)
1462+
return self.to_string(**repr_params)
14861463

14871464
def to_string(
14881465
self,

pandas/io/formats/format.py

+40
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ def get_dataframe_repr_params() -> dict[str, Any]:
489489
Supplying these parameters to DataFrame.to_string is equivalent to calling
490490
``repr(DataFrame)``. This is useful if you want to adjust the repr output.
491491
492+
.. versionadded:: 1.4.0
493+
492494
Example
493495
-------
494496
>>> import pandas as pd
@@ -514,6 +516,44 @@ def get_dataframe_repr_params() -> dict[str, Any]:
514516
}
515517

516518

519+
def get_series_repr_params(series: Series) -> dict[str, Any]:
520+
"""Get the parameters used to repr(Series) calls using Series.to_string.
521+
522+
Supplying these parameters to Series.to_string is equivalent to calling
523+
``repr(series)``. This is useful if you want to adjust the series repr output.
524+
525+
.. versionadded:: 1.4.0
526+
527+
Example
528+
-------
529+
>>> import pandas as pd
530+
>>>
531+
>>> ser = pd.Series([1, 2, 3, 4])
532+
>>> repr_params = pd.io.formats.format.get_series_repr_params(ser)
533+
>>> repr(ser) == ser.to_string(**repr_params)
534+
True
535+
"""
536+
width, height = get_terminal_size()
537+
max_rows = (
538+
height
539+
if get_option("display.max_rows") == 0
540+
else get_option("display.max_rows")
541+
)
542+
min_rows = (
543+
height
544+
if get_option("display.max_rows") == 0
545+
else get_option("display.min_rows")
546+
)
547+
548+
return {
549+
"name": series.name,
550+
"dtype": series.dtype,
551+
"min_rows": min_rows,
552+
"max_rows": max_rows,
553+
"length": get_option("display.show_dimensions"),
554+
}
555+
556+
517557
class DataFrameFormatter:
518558
"""Class for processing dataframe formatting options and data."""
519559

0 commit comments

Comments
 (0)