From ac113cec66442382077cd2af3dc74904e176967c Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 9 Aug 2021 09:47:32 -0700 Subject: [PATCH 1/2] REF: format_object_attrs -> Index._format_attrs --- pandas/core/indexes/base.py | 18 ++++++++++++++-- pandas/io/formats/printing.py | 39 ----------------------------------- 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0d27114161916..3586dc54d1452 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, ) @@ -1183,7 +1184,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 = [] + + 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__""" From 6d5b05b80ff74933059e49577d90c708553979d7 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 10 Aug 2021 11:58:03 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 24ec6270c3ebc..ee943c3a327f3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1215,7 +1215,7 @@ def _format_attrs(self) -> list[tuple[str_t, str_t | int]]: """ Return a list of tuples of the (attr,formatted_value). """ - attrs = [] + attrs: list[tuple[str_t, str_t | int]] = [] if not self._is_multi: attrs.append(("dtype", f"'{self.dtype}'"))