Skip to content

Commit be1f29a

Browse files
jbrockmendelTLouf
authored andcommitted
REF: de-duplicate _format_attrs (pandas-dev#41655)
1 parent f0be3d2 commit be1f29a

File tree

5 files changed

+21
-74
lines changed

5 files changed

+21
-74
lines changed

pandas/core/indexes/base.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1158,14 +1158,18 @@ def _format_data(self, name=None) -> str_t:
11581158
is_justify = False
11591159

11601160
return format_object_summary(
1161-
self, self._formatter_func, is_justify=is_justify, name=name
1161+
self,
1162+
self._formatter_func,
1163+
is_justify=is_justify,
1164+
name=name,
1165+
line_break_each_value=self._is_multi,
11621166
)
11631167

1164-
def _format_attrs(self):
1168+
def _format_attrs(self) -> list[tuple[str_t, str_t | int]]:
11651169
"""
11661170
Return a list of tuples of the (attr,formatted_value).
11671171
"""
1168-
return format_object_attrs(self)
1172+
return format_object_attrs(self, include_dtype=not self._is_multi)
11691173

11701174
def _mpl_repr(self):
11711175
# how to represent ourselves to matplotlib
@@ -2407,6 +2411,13 @@ def is_all_dates(self) -> bool:
24072411
)
24082412
return self._is_all_dates
24092413

2414+
@cache_readonly
2415+
def _is_multi(self) -> bool:
2416+
"""
2417+
Cached check equivalent to isinstance(self, MultiIndex)
2418+
"""
2419+
return isinstance(self, ABCMultiIndex)
2420+
24102421
# --------------------------------------------------------------------
24112422
# Pickle Methods
24122423

pandas/core/indexes/category.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,8 @@ def _format_attrs(self):
335335
# error: "CategoricalIndex" has no attribute "ordered"
336336
("ordered", self.ordered), # type: ignore[attr-defined]
337337
]
338-
if self.name is not None:
339-
attrs.append(("name", ibase.default_pprint(self.name)))
340-
attrs.append(("dtype", f"'{self.dtype.name}'"))
341-
max_seq_items = get_option("display.max_seq_items") or len(self)
342-
if len(self) > max_seq_items:
343-
attrs.append(("length", len(self)))
344-
return attrs
338+
extra = super()._format_attrs()
339+
return attrs + extra
345340

346341
def _format_with_header(self, header: list[str], na_rep: str = "NaN") -> list[str]:
347342
from pandas.io.formats.printing import pprint_thing

pandas/core/indexes/datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ def _format_attrs(self):
361361
freq = self.freqstr
362362
if freq is not None:
363363
freq = repr(freq)
364-
attrs.append(("freq", freq))
364+
# Argument 1 to "append" of "list" has incompatible type
365+
# "Tuple[str, Optional[str]]"; expected "Tuple[str, Union[str, int]]"
366+
attrs.append(("freq", freq)) # type: ignore[arg-type]
365367
return attrs
366368

367369
def _summary(self, name=None) -> str:

pandas/core/indexes/interval.py

+1-44
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import numpy as np
1818

19-
from pandas._config import get_option
20-
2119
from pandas._libs import lib
2220
from pandas._libs.interval import (
2321
Interval,
@@ -80,7 +78,6 @@
8078
from pandas.core.indexes.base import (
8179
Index,
8280
_index_shared_docs,
83-
default_pprint,
8481
ensure_index,
8582
maybe_extract_name,
8683
)
@@ -919,49 +916,9 @@ def _format_native_types(self, na_rep="NaN", quoting=None, **kwargs):
919916
return super()._format_native_types(na_rep=na_rep, quoting=quoting, **kwargs)
920917

921918
def _format_data(self, name=None) -> str:
922-
923919
# TODO: integrate with categorical and make generic
924920
# name argument is unused here; just for compat with base / categorical
925-
n = len(self)
926-
max_seq_items = min((get_option("display.max_seq_items") or n) // 10, 10)
927-
928-
formatter = str
929-
930-
if n == 0:
931-
summary = "[]"
932-
elif n == 1:
933-
first = formatter(self[0])
934-
summary = f"[{first}]"
935-
elif n == 2:
936-
first = formatter(self[0])
937-
last = formatter(self[-1])
938-
summary = f"[{first}, {last}]"
939-
else:
940-
941-
if n > max_seq_items:
942-
n = min(max_seq_items // 2, 10)
943-
head = [formatter(x) for x in self[:n]]
944-
tail = [formatter(x) for x in self[-n:]]
945-
head_joined = ", ".join(head)
946-
tail_joined = ", ".join(tail)
947-
summary = f"[{head_joined} ... {tail_joined}]"
948-
else:
949-
tail = [formatter(x) for x in self]
950-
joined = ", ".join(tail)
951-
summary = f"[{joined}]"
952-
953-
return summary + "," + self._format_space()
954-
955-
def _format_attrs(self):
956-
attrs = []
957-
if self.name is not None:
958-
attrs.append(("name", default_pprint(self.name)))
959-
attrs.append(("dtype", f"'{self.dtype}'"))
960-
return attrs
961-
962-
def _format_space(self) -> str:
963-
space = " " * (len(type(self).__name__) + 1)
964-
return f"\n{space}"
921+
return self._data._format_data() + "," + self._format_space()
965922

966923
# --------------------------------------------------------------------
967924
# Set Operations

pandas/core/indexes/multi.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@
8989
lexsort_indexer,
9090
)
9191

92-
from pandas.io.formats.printing import (
93-
format_object_attrs,
94-
format_object_summary,
95-
pprint_thing,
96-
)
92+
from pandas.io.formats.printing import pprint_thing
9793

9894
if TYPE_CHECKING:
9995
from pandas import (
@@ -1287,20 +1283,6 @@ def _formatter_func(self, tup):
12871283
formatter_funcs = [level._formatter_func for level in self.levels]
12881284
return tuple(func(val) for func, val in zip(formatter_funcs, tup))
12891285

1290-
def _format_data(self, name=None) -> str:
1291-
"""
1292-
Return the formatted data as a unicode string
1293-
"""
1294-
return format_object_summary(
1295-
self, self._formatter_func, name=name, line_break_each_value=True
1296-
)
1297-
1298-
def _format_attrs(self):
1299-
"""
1300-
Return a list of tuples of the (attr,formatted_value).
1301-
"""
1302-
return format_object_attrs(self, include_dtype=False)
1303-
13041286
def _format_native_types(self, na_rep="nan", **kwargs):
13051287
new_levels = []
13061288
new_codes = []

0 commit comments

Comments
 (0)