Skip to content

REF: de-duplicate IntervalArray formatting functions #55469

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 1 commit into from
Oct 10, 2023
Merged
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
43 changes: 4 additions & 39 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import numpy as np

from pandas._config import get_option

from pandas._libs import lib
from pandas._libs.interval import (
VALID_CLOSED,
Expand Down Expand Up @@ -1233,43 +1231,10 @@ def value_counts(self, dropna: bool = True) -> Series:
# ---------------------------------------------------------------------
# Rendering Methods

def _format_data(self) -> str:
# TODO: integrate with categorical and make generic
n = len(self)
max_seq_items = min((get_option("display.max_seq_items") or n) // 10, 10)

formatter = str

if n == 0:
summary = "[]"
elif n == 1:
first = formatter(self[0])
summary = f"[{first}]"
elif n == 2:
first = formatter(self[0])
last = formatter(self[-1])
summary = f"[{first}, {last}]"
else:
if n > max_seq_items:
n = min(max_seq_items // 2, 10)
head = [formatter(x) for x in self[:n]]
tail = [formatter(x) for x in self[-n:]]
head_str = ", ".join(head)
tail_str = ", ".join(tail)
summary = f"[{head_str} ... {tail_str}]"
else:
tail = [formatter(x) for x in self]
tail_str = ", ".join(tail)
summary = f"[{tail_str}]"

return summary

def __repr__(self) -> str:
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 _formatter(self, boxed: bool = False):
# returning 'str' here causes us to render as e.g. "(0, 1]" instead of
# "Interval(0, 1, closed='right')"
return str

# ---------------------------------------------------------------------
# Vectorized Interval Properties/Attributes
Expand Down