Skip to content

ENH: EA._get_repr_footer #55478

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 11, 2023
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
12 changes: 10 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,14 @@ def __repr__(self) -> str:
self, self._formatter(), indent_for_name=False
).rstrip(", \n")
class_name = f"<{type(self).__name__}>\n"
return f"{class_name}{data}\nLength: {len(self)}, dtype: {self.dtype}"
footer = self._get_repr_footer()
return f"{class_name}{data}\n{footer}"

def _get_repr_footer(self) -> str:
# GH#24278
if self.ndim > 1:
return f"Shape: {self.shape}, dtype: {self.dtype}"
return f"Length: {len(self)}, dtype: {self.dtype}"

def _repr_2d(self) -> str:
from pandas.io.formats.printing import format_object_summary
Expand All @@ -1679,7 +1686,8 @@ def _repr_2d(self) -> str:
]
data = ",\n".join(lines)
class_name = f"<{type(self).__name__}>"
return f"{class_name}\n[\n{data}\n]\nShape: {self.shape}, dtype: {self.dtype}"
footer = self._get_repr_footer()
return f"{class_name}\n[\n{data}\n]\n{footer}"

def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,7 @@ def _repr_categories(self) -> list[str]:
category_strs = [x.strip() for x in category_strs]
return category_strs

def _repr_categories_info(self) -> str:
def _get_repr_footer(self) -> str:
"""
Returns a string representation of the footer.
"""
Expand Down Expand Up @@ -2229,7 +2229,7 @@ def __repr__(self) -> str:
"""
String representation.
"""
footer = self._repr_categories_info()
footer = self._get_repr_footer()
length = len(self)
max_len = 10
if length > max_len:
Expand Down
13 changes: 7 additions & 6 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@ def _get_footer(self) -> str:
name = self.series.name
footer = ""

if getattr(self.series.index, "freq", None) is not None:
assert isinstance(
self.series.index, (DatetimeIndex, PeriodIndex, TimedeltaIndex)
)
footer += f"Freq: {self.series.index.freqstr}"
index = self.series.index
if (
isinstance(index, (DatetimeIndex, PeriodIndex, TimedeltaIndex))
and index.freq is not None
):
footer += f"Freq: {index.freqstr}"
Copy link
Member

@mroeschke mroeschke Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to include this in DTA/TDA/PA so we can avoid the special casing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im hoping to get the special casing out of here eventually, just not this pass


if self.name is not False and name is not None:
if footer:
Expand All @@ -289,7 +290,7 @@ def _get_footer(self) -> str:
# level infos are added to the end and in a new line, like it is done
# for Categoricals
if isinstance(self.tr_series.dtype, CategoricalDtype):
level_info = self.tr_series._values._repr_categories_info()
level_info = self.tr_series._values._get_repr_footer()
if footer:
footer += "\n"
footer += level_info
Expand Down