Skip to content

REF: extract params used in Series.__repr__ #44218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""
from __future__ import annotations

from io import StringIO
from shutil import get_terminal_size
from textwrap import dedent
from typing import (
IO,
Expand Down Expand Up @@ -1460,29 +1458,8 @@ def __repr__(self) -> str:
"""
Return a string representation for a particular Series.
"""
buf = StringIO("")
width, height = get_terminal_size()
max_rows = (
height
if get_option("display.max_rows") == 0
else get_option("display.max_rows")
)
min_rows = (
height
if get_option("display.max_rows") == 0
else get_option("display.min_rows")
)
show_dimensions = get_option("display.show_dimensions")

self.to_string(
buf=buf,
name=self.name,
dtype=self.dtype,
min_rows=min_rows,
max_rows=max_rows,
length=show_dimensions,
)
return buf.getvalue()
repr_params = fmt.get_series_repr_params(self)
return self.to_string(**repr_params)

def to_string(
self,
Expand Down
40 changes: 40 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ def get_dataframe_repr_params() -> dict[str, Any]:
Supplying these parameters to DataFrame.to_string is equivalent to calling
``repr(DataFrame)``. This is useful if you want to adjust the repr output.

.. versionadded:: 1.4.0

Example
-------
>>> import pandas as pd
Expand All @@ -514,6 +516,44 @@ def get_dataframe_repr_params() -> dict[str, Any]:
}


def get_series_repr_params(series: Series) -> dict[str, Any]:
"""Get the parameters used to repr(Series) calls using Series.to_string.

Supplying these parameters to Series.to_string is equivalent to calling
``repr(series)``. This is useful if you want to adjust the series repr output.

.. versionadded:: 1.4.0

Example
-------
>>> import pandas as pd
>>>
>>> ser = pd.Series([1, 2, 3, 4])
>>> repr_params = pd.io.formats.format.get_series_repr_params(ser)
>>> repr(ser) == ser.to_string(**repr_params)
True
"""
width, height = get_terminal_size()
max_rows = (
height
if get_option("display.max_rows") == 0
else get_option("display.max_rows")
)
min_rows = (
height
if get_option("display.max_rows") == 0
else get_option("display.min_rows")
)

return {
"name": series.name,
"dtype": series.dtype,
"min_rows": min_rows,
"max_rows": max_rows,
"length": get_option("display.show_dimensions"),
}


class DataFrameFormatter:
"""Class for processing dataframe formatting options and data."""

Expand Down