From a55e32d21bd3e3c7c2942f14925d05bb168a09ea Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 6 Oct 2023 11:15:07 -0700 Subject: [PATCH 1/2] REF: remove IntervalIndex._format_data, _format_space --- pandas/core/arrays/interval.py | 8 -------- pandas/core/indexes/base.py | 13 ++----------- pandas/core/indexes/interval.py | 5 ----- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 58ade1ee935ec..b55a40f5488ea 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1235,7 +1235,6 @@ def value_counts(self, dropna: bool = True) -> Series: def _format_data(self) -> str: # TODO: integrate with categorical and make generic - # name argument is unused here; just for compat with base / categorical n = len(self) max_seq_items = min((get_option("display.max_seq_items") or n) // 10, 10) @@ -1266,19 +1265,12 @@ def _format_data(self) -> str: return summary def __repr__(self) -> str: - # the short repr has no trailing newline, while the truncated - # repr does. So we include a newline in our template, and strip - # any trailing newlines from format_object_summary data = self._format_data() class_name = f"<{type(self).__name__}>\n" template = f"{class_name}{data}\nLength: {len(self)}, dtype: {self.dtype}" return template - def _format_space(self) -> str: - space = " " * (len(type(self).__name__) + 1) - return f"\n{space}" - # --------------------------------------------------------------------- # Vectorized Interval Properties/Attributes diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 147113a1e505e..34427a9c02d40 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1284,25 +1284,16 @@ def __repr__(self) -> str_t: klass_name = type(self).__name__ data = self._format_data() attrs = self._format_attrs() - space = self._format_space() attrs_str = [f"{k}={v}" for k, v in attrs] - prepr = f",{space}".join(attrs_str) + prepr = ", ".join(attrs_str) # no data provided, just attributes if data is None: + # i.e. RangeIndex data = "" return f"{klass_name}({data}{prepr})" - def _format_space(self) -> str_t: - # using space here controls if the attributes - # are line separated or not (the default) - - # max_seq_items = get_option('display.max_seq_items') - # if len(self) > max_seq_items: - # space = "\n%s" % (' ' * (len(klass) + 1)) - return " " - @property def _formatter_func(self): """ diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index ae027ea68a525..616539ea09708 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -850,11 +850,6 @@ def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]: # matches base class except for whitespace padding return header + list(self._format_native_types(na_rep=na_rep)) - def _format_data(self, name=None) -> str: - # TODO: integrate with categorical and make generic - # name argument is unused here; just for compat with base / categorical - return f"{self._data._format_data()},{self._format_space()}" - # -------------------------------------------------------------------- # Set Operations From a0aaa19fcf5a0f900322fafe4121f8533020bcdf Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 6 Oct 2023 16:20:06 -0700 Subject: [PATCH 2/2] improve check, remove outdated comment --- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/interval.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 34427a9c02d40..c3cab965041e0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1310,7 +1310,7 @@ def _format_data(self, name=None) -> str_t: if self.inferred_type == "string": is_justify = False - elif self.inferred_type == "categorical": + elif isinstance(self.dtype, CategoricalDtype): self = cast("CategoricalIndex", self) if is_object_dtype(self.categories.dtype): is_justify = False diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 616539ea09708..209ac84869e85 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -844,7 +844,6 @@ def length(self) -> Index: # -------------------------------------------------------------------- # Rendering Methods - # __repr__ associated methods are based on MultiIndex def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]: # matches base class except for whitespace padding