Skip to content

Commit 7e8148f

Browse files
authored
REF: De-special-case _format_with_header (#55491)
* REF: De-special-case _format_with_header * simplify
1 parent c9a98f0 commit 7e8148f

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

pandas/core/indexes/base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,12 +1387,20 @@ def _format_with_header(self, *, header: list[str_t], na_rep: str_t) -> list[str
13871387

13881388
values = self._values
13891389

1390-
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
1391-
values = np.asarray(values)
1390+
if (
1391+
is_object_dtype(values.dtype)
1392+
or is_string_dtype(values.dtype)
1393+
or isinstance(self.dtype, (IntervalDtype, CategoricalDtype))
1394+
):
13921395
# TODO: why do we need different justify for these cases?
1393-
result = trim_front(format_array(values, None, justify="all"))
1396+
justify = "all"
13941397
else:
1395-
result = trim_front(format_array(values, None, justify="left"))
1398+
justify = "left"
1399+
# passing leading_space=False breaks test_format_missing,
1400+
# test_index_repr_in_frame_with_nan, but would otherwise make
1401+
# trim_front unnecessary
1402+
formatted = format_array(values, None, justify=justify)
1403+
result = trim_front(formatted)
13961404
return header + result
13971405

13981406
def _format_native_types(

pandas/core/indexes/category.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from pandas.core.dtypes.missing import (
2222
is_valid_na_for_dtype,
2323
isna,
24-
notna,
2524
)
2625

2726
from pandas.core.arrays.categorical import (
@@ -38,8 +37,6 @@
3837
inherit_names,
3938
)
4039

41-
from pandas.io.formats.printing import pprint_thing
42-
4340
if TYPE_CHECKING:
4441
from collections.abc import Hashable
4542

@@ -356,13 +353,6 @@ def _format_attrs(self):
356353
extra = super()._format_attrs()
357354
return attrs + extra
358355

359-
def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
360-
result = [
361-
pprint_thing(x, escape_chars=("\t", "\r", "\n")) if notna(x) else na_rep
362-
for x in self._values
363-
]
364-
return header + result
365-
366356
# --------------------------------------------------------------------
367357

368358
@property

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def format(
216216
def _format_with_header(
217217
self, *, header: list[str], na_rep: str, date_format: str | None = None
218218
) -> list[str]:
219+
# TODO: not reached in tests 2023-10-11
219220
# matches base class except for whitespace padding and date_format
220221
return header + list(
221222
self._format_native_types(na_rep=na_rep, date_format=date_format)

pandas/core/indexes/interval.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,6 @@ def mid(self) -> Index:
842842
def length(self) -> Index:
843843
return Index(self._data.length, copy=False)
844844

845-
# --------------------------------------------------------------------
846-
# Rendering Methods
847-
848-
def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
849-
# matches base class except for whitespace padding
850-
return header + list(self._format_native_types(na_rep=na_rep))
851-
852845
# --------------------------------------------------------------------
853846
# Set Operations
854847

0 commit comments

Comments
 (0)