Skip to content

Commit 4870b49

Browse files
jbrockmendelfeefladder
authored andcommitted
REF: format_object_attrs -> Index._format_attrs (pandas-dev#42953)
1 parent 63cc520 commit 4870b49

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

pandas/core/indexes/base.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import numpy as np
2222

23+
from pandas._config import get_option
24+
2325
from pandas._libs import (
2426
algos as libalgos,
2527
index as libindex,
@@ -156,7 +158,6 @@
156158
from pandas.io.formats.printing import (
157159
PrettyDict,
158160
default_pprint,
159-
format_object_attrs,
160161
format_object_summary,
161162
pprint_thing,
162163
)
@@ -1214,7 +1215,20 @@ def _format_attrs(self) -> list[tuple[str_t, str_t | int]]:
12141215
"""
12151216
Return a list of tuples of the (attr,formatted_value).
12161217
"""
1217-
return format_object_attrs(self, include_dtype=not self._is_multi)
1218+
attrs: list[tuple[str_t, str_t | int]] = []
1219+
1220+
if not self._is_multi:
1221+
attrs.append(("dtype", f"'{self.dtype}'"))
1222+
1223+
if self.name is not None:
1224+
attrs.append(("name", default_pprint(self.name)))
1225+
elif self._is_multi and any(x is not None for x in self.names):
1226+
attrs.append(("names", default_pprint(self.names)))
1227+
1228+
max_seq_items = get_option("display.max_seq_items") or len(self)
1229+
if len(self) > max_seq_items:
1230+
attrs.append(("length", len(self)))
1231+
return attrs
12181232

12191233
@final
12201234
def _mpl_repr(self) -> np.ndarray:

pandas/io/formats/printing.py

-39
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Iterable,
1212
Mapping,
1313
Sequence,
14-
Sized,
1514
TypeVar,
1615
Union,
1716
)
@@ -505,44 +504,6 @@ def _justify(
505504
return head, tail # type: ignore[return-value]
506505

507506

508-
def format_object_attrs(
509-
obj: Sized, include_dtype: bool = True
510-
) -> list[tuple[str, str | int]]:
511-
"""
512-
Return a list of tuples of the (attr, formatted_value)
513-
for common attrs, including dtype, name, length
514-
515-
Parameters
516-
----------
517-
obj : object
518-
Must be sized.
519-
include_dtype : bool
520-
If False, dtype won't be in the returned list
521-
522-
Returns
523-
-------
524-
list of 2-tuple
525-
526-
"""
527-
attrs: list[tuple[str, str | int]] = []
528-
if hasattr(obj, "dtype") and include_dtype:
529-
# error: "Sized" has no attribute "dtype"
530-
attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined]
531-
if getattr(obj, "name", None) is not None:
532-
# error: "Sized" has no attribute "name"
533-
attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined]
534-
# error: "Sized" has no attribute "names"
535-
elif getattr(obj, "names", None) is not None and any(
536-
obj.names # type: ignore[attr-defined]
537-
):
538-
# error: "Sized" has no attribute "names"
539-
attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined]
540-
max_seq_items = get_option("display.max_seq_items") or len(obj)
541-
if len(obj) > max_seq_items:
542-
attrs.append(("length", len(obj)))
543-
return attrs
544-
545-
546507
class PrettyDict(Dict[_KT, _VT]):
547508
"""Dict extension to support abbreviated __repr__"""
548509

0 commit comments

Comments
 (0)