Skip to content

REF: format_object_attrs -> Index._format_attrs #42953

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 3 commits into from
Aug 10, 2021
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
18 changes: 16 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import numpy as np

from pandas._config import get_option

from pandas._libs import (
algos as libalgos,
index as libindex,
Expand Down Expand Up @@ -156,7 +158,6 @@
from pandas.io.formats.printing import (
PrettyDict,
default_pprint,
format_object_attrs,
format_object_summary,
pprint_thing,
)
Expand Down Expand Up @@ -1214,7 +1215,20 @@ def _format_attrs(self) -> list[tuple[str_t, str_t | int]]:
"""
Return a list of tuples of the (attr,formatted_value).
"""
return format_object_attrs(self, include_dtype=not self._is_multi)
attrs: list[tuple[str_t, str_t | int]] = []

if not self._is_multi:
attrs.append(("dtype", f"'{self.dtype}'"))

if self.name is not None:
attrs.append(("name", default_pprint(self.name)))
elif self._is_multi and any(x is not None for x in self.names):
attrs.append(("names", default_pprint(self.names)))

max_seq_items = get_option("display.max_seq_items") or len(self)
if len(self) > max_seq_items:
attrs.append(("length", len(self)))
return attrs

@final
def _mpl_repr(self) -> np.ndarray:
Expand Down
39 changes: 0 additions & 39 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Iterable,
Mapping,
Sequence,
Sized,
TypeVar,
Union,
)
Expand Down Expand Up @@ -505,44 +504,6 @@ def _justify(
return head, tail # type: ignore[return-value]


def format_object_attrs(
obj: Sized, include_dtype: bool = True
) -> list[tuple[str, str | int]]:
"""
Return a list of tuples of the (attr, formatted_value)
for common attrs, including dtype, name, length

Parameters
----------
obj : object
Must be sized.
include_dtype : bool
If False, dtype won't be in the returned list

Returns
-------
list of 2-tuple

"""
attrs: list[tuple[str, str | int]] = []
if hasattr(obj, "dtype") and include_dtype:
# error: "Sized" has no attribute "dtype"
attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined]
if getattr(obj, "name", None) is not None:
# error: "Sized" has no attribute "name"
attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined]
# error: "Sized" has no attribute "names"
elif getattr(obj, "names", None) is not None and any(
obj.names # type: ignore[attr-defined]
):
# error: "Sized" has no attribute "names"
attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined]
max_seq_items = get_option("display.max_seq_items") or len(obj)
if len(obj) > max_seq_items:
attrs.append(("length", len(obj)))
return attrs


class PrettyDict(Dict[_KT, _VT]):
"""Dict extension to support abbreviated __repr__"""

Expand Down