diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 08946bf5a8033..ee943c3a327f3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -20,6 +20,8 @@ import numpy as np +from pandas._config import get_option + from pandas._libs import ( algos as libalgos, index as libindex, @@ -156,7 +158,6 @@ from pandas.io.formats.printing import ( PrettyDict, default_pprint, - format_object_attrs, format_object_summary, pprint_thing, ) @@ -1214,7 +1215,20 @@ def _format_attrs(self) -> list[tuple[str_t, str_t | int]]: """ Return a list of tuples of the (attr,formatted_value). """ - return format_object_attrs(self, include_dtype=not self._is_multi) + attrs: list[tuple[str_t, str_t | int]] = [] + + if not self._is_multi: + attrs.append(("dtype", f"'{self.dtype}'")) + + if self.name is not None: + attrs.append(("name", default_pprint(self.name))) + elif self._is_multi and any(x is not None for x in self.names): + attrs.append(("names", default_pprint(self.names))) + + max_seq_items = get_option("display.max_seq_items") or len(self) + if len(self) > max_seq_items: + attrs.append(("length", len(self))) + return attrs @final def _mpl_repr(self) -> np.ndarray: diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index ac81fffcf353a..77431533e703a 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -11,7 +11,6 @@ Iterable, Mapping, Sequence, - Sized, TypeVar, Union, ) @@ -505,44 +504,6 @@ def _justify( return head, tail # type: ignore[return-value] -def format_object_attrs( - obj: Sized, include_dtype: bool = True -) -> list[tuple[str, str | int]]: - """ - Return a list of tuples of the (attr, formatted_value) - for common attrs, including dtype, name, length - - Parameters - ---------- - obj : object - Must be sized. - include_dtype : bool - If False, dtype won't be in the returned list - - Returns - ------- - list of 2-tuple - - """ - attrs: list[tuple[str, str | int]] = [] - if hasattr(obj, "dtype") and include_dtype: - # error: "Sized" has no attribute "dtype" - attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined] - if getattr(obj, "name", None) is not None: - # error: "Sized" has no attribute "name" - attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined] - # error: "Sized" has no attribute "names" - elif getattr(obj, "names", None) is not None and any( - obj.names # type: ignore[attr-defined] - ): - # error: "Sized" has no attribute "names" - attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined] - max_seq_items = get_option("display.max_seq_items") or len(obj) - if len(obj) > max_seq_items: - attrs.append(("length", len(obj))) - return attrs - - class PrettyDict(Dict[_KT, _VT]): """Dict extension to support abbreviated __repr__"""