Skip to content

REF: De-special-case _format_with_header #55491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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