Skip to content

Commit

Permalink
REF: de-duplicate IntervalArray formatting functions (#55469)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Oct 10, 2023
1 parent f7b49d8 commit 1025151
Showing 1 changed file with 4 additions and 39 deletions.
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

0 comments on commit 1025151

Please sign in to comment.