Skip to content

Commit

Permalink
REF: De-special-case _format_with_header (#55491)
Browse files Browse the repository at this point in the history
* REF: De-special-case _format_with_header

* simplify
  • Loading branch information
jbrockmendel authored Oct 12, 2023
1 parent c9a98f0 commit 7e8148f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
16 changes: 12 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,12 +1387,20 @@ def _format_with_header(self, *, header: list[str_t], na_rep: str_t) -> list[str

values = self._values

if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
values = np.asarray(values)
if (
is_object_dtype(values.dtype)
or is_string_dtype(values.dtype)
or isinstance(self.dtype, (IntervalDtype, CategoricalDtype))
):
# TODO: why do we need different justify for these cases?
result = trim_front(format_array(values, None, justify="all"))
justify = "all"
else:
result = trim_front(format_array(values, None, justify="left"))
justify = "left"
# passing leading_space=False breaks test_format_missing,
# test_index_repr_in_frame_with_nan, but would otherwise make
# trim_front unnecessary
formatted = format_array(values, None, justify=justify)
result = trim_front(formatted)
return header + result

def _format_native_types(
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from pandas.core.dtypes.missing import (
is_valid_na_for_dtype,
isna,
notna,
)

from pandas.core.arrays.categorical import (
Expand All @@ -38,8 +37,6 @@
inherit_names,
)

from pandas.io.formats.printing import pprint_thing

if TYPE_CHECKING:
from collections.abc import Hashable

Expand Down Expand Up @@ -356,13 +353,6 @@ def _format_attrs(self):
extra = super()._format_attrs()
return attrs + extra

def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
result = [
pprint_thing(x, escape_chars=("\t", "\r", "\n")) if notna(x) else na_rep
for x in self._values
]
return header + result

# --------------------------------------------------------------------

@property
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def format(
def _format_with_header(
self, *, header: list[str], na_rep: str, date_format: str | None = None
) -> list[str]:
# TODO: not reached in tests 2023-10-11
# matches base class except for whitespace padding and date_format
return header + list(
self._format_native_types(na_rep=na_rep, date_format=date_format)
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,13 +842,6 @@ def mid(self) -> Index:
def length(self) -> Index:
return Index(self._data.length, copy=False)

# --------------------------------------------------------------------
# Rendering Methods

def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
# matches base class except for whitespace padding
return header + list(self._format_native_types(na_rep=na_rep))

# --------------------------------------------------------------------
# Set Operations

Expand Down

0 comments on commit 7e8148f

Please sign in to comment.