From 8b0577f1101d13a6baf46aa01cb31a3b52276b75 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 10 Oct 2023 14:23:33 -0700 Subject: [PATCH] ENH: EA._get_repr_footer --- pandas/core/arrays/base.py | 12 ++++++++++-- pandas/core/arrays/categorical.py | 4 ++-- pandas/io/formats/format.py | 13 +++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 31c143ee012bb..e7b7ecba60e0b 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -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 @@ -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]: """ diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 5059f5d000ccd..e19635af6ab0b 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -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. """ @@ -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: diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index cac83e2a48972..322f7f88494de 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -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: @@ -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