Skip to content

Commit

Permalink
REF: remove redundant Index formatting methods (#55432)
Browse files Browse the repository at this point in the history
* REF: remove IntervalIndex._format_data, _format_space

* improve check, remove outdated comment
  • Loading branch information
jbrockmendel authored Oct 9, 2023
1 parent 69cd075 commit fa96a52
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 26 deletions.
8 changes: 0 additions & 8 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,6 @@ def value_counts(self, dropna: bool = True) -> Series:

def _format_data(self) -> str:
# TODO: integrate with categorical and make generic
# name argument is unused here; just for compat with base / categorical
n = len(self)
max_seq_items = min((get_option("display.max_seq_items") or n) // 10, 10)

Expand Down Expand Up @@ -1266,19 +1265,12 @@ def _format_data(self) -> str:
return summary

def __repr__(self) -> str:
# the short repr has no trailing newline, while the truncated
# repr does. So we include a newline in our template, and strip
# any trailing newlines from format_object_summary
data = self._format_data()
class_name = f"<{type(self).__name__}>\n"

template = f"{class_name}{data}\nLength: {len(self)}, dtype: {self.dtype}"
return template

def _format_space(self) -> str:
space = " " * (len(type(self).__name__) + 1)
return f"\n{space}"

# ---------------------------------------------------------------------
# Vectorized Interval Properties/Attributes

Expand Down
15 changes: 3 additions & 12 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,25 +1284,16 @@ def __repr__(self) -> str_t:
klass_name = type(self).__name__
data = self._format_data()
attrs = self._format_attrs()
space = self._format_space()
attrs_str = [f"{k}={v}" for k, v in attrs]
prepr = f",{space}".join(attrs_str)
prepr = ", ".join(attrs_str)

# no data provided, just attributes
if data is None:
# i.e. RangeIndex
data = ""

return f"{klass_name}({data}{prepr})"

def _format_space(self) -> str_t:
# using space here controls if the attributes
# are line separated or not (the default)

# max_seq_items = get_option('display.max_seq_items')
# if len(self) > max_seq_items:
# space = "\n%s" % (' ' * (len(klass) + 1))
return " "

@property
def _formatter_func(self):
"""
Expand All @@ -1319,7 +1310,7 @@ def _format_data(self, name=None) -> str_t:

if self.inferred_type == "string":
is_justify = False
elif self.inferred_type == "categorical":
elif isinstance(self.dtype, CategoricalDtype):
self = cast("CategoricalIndex", self)
if is_object_dtype(self.categories.dtype):
is_justify = False
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,17 +844,11 @@ def length(self) -> Index:

# --------------------------------------------------------------------
# Rendering Methods
# __repr__ associated methods are based on MultiIndex

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))

def _format_data(self, name=None) -> str:
# TODO: integrate with categorical and make generic
# name argument is unused here; just for compat with base / categorical
return f"{self._data._format_data()},{self._format_space()}"

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

Expand Down

0 comments on commit fa96a52

Please sign in to comment.