Skip to content

Commit

Permalink
ENH: EA._get_repr_footer
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Oct 10, 2023
1 parent 66a54a3 commit 8b0577f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
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}"

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

0 comments on commit 8b0577f

Please sign in to comment.