Skip to content

IntervalIndex/IntervalArray __repr__ remove redundant closed #39085

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
Jan 11, 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
22 changes: 9 additions & 13 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
>>> pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)])
<IntervalArray>
[(0, 1], (1, 5]]
Length: 2, closed: right, dtype: interval[int64, right]
Length: 2, dtype: interval[int64, right]

It may also be constructed using one of the constructor
methods: :meth:`IntervalArray.from_arrays`,
Expand Down Expand Up @@ -354,7 +354,7 @@ def _from_factorized(cls, values, original):
>>> pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3])
<IntervalArray>
[(0, 1], (1, 2], (2, 3]]
Length: 3, closed: right, dtype: interval[int64, right]
Length: 3, dtype: interval[int64, right]
"""
),
}
Expand Down Expand Up @@ -425,7 +425,7 @@ def from_breaks(
>>> pd.arrays.IntervalArray.from_arrays([0, 1, 2], [1, 2, 3])
<IntervalArray>
[(0, 1], (1, 2], (2, 3]]
Length: 3, closed: right, dtype: interval[int64, right]
Length: 3, dtype: interval[int64, right]
"""
),
}
Expand Down Expand Up @@ -484,7 +484,7 @@ def from_arrays(
>>> pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)])
<IntervalArray>
[(0, 1], (1, 2]]
Length: 2, closed: right, dtype: interval[int64, right]
Length: 2, dtype: interval[int64, right]
"""
),
}
Expand Down Expand Up @@ -1071,11 +1071,7 @@ def __repr__(self) -> str:
data = self._format_data()
class_name = f"<{type(self).__name__}>\n"

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

def _format_space(self):
Expand Down Expand Up @@ -1185,7 +1181,7 @@ def mid(self):
>>> intervals
<IntervalArray>
[(0, 1], (1, 3], (2, 4]]
Length: 3, closed: right, dtype: interval[int64, right]
Length: 3, dtype: interval[int64, right]
"""
),
}
Expand Down Expand Up @@ -1249,11 +1245,11 @@ def closed(self):
>>> index
<IntervalArray>
[(0, 1], (1, 2], (2, 3]]
Length: 3, closed: right, dtype: interval[int64, right]
Length: 3, dtype: interval[int64, right]
>>> index.set_closed('both')
<IntervalArray>
[[0, 1], [1, 2], [2, 3]]
Length: 3, closed: both, dtype: interval[int64, both]
Length: 3, dtype: interval[int64, both]
"""
),
}
Expand Down Expand Up @@ -1452,7 +1448,7 @@ def repeat(self, repeats, axis=None):
>>> intervals
<IntervalArray>
[(0, 1], (1, 3], (2, 4]]
Length: 3, closed: right, dtype: interval[int64, right]
Length: 3, dtype: interval[int64, right]
"""
),
}
Expand Down
30 changes: 7 additions & 23 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
default_pprint,
ensure_index,
maybe_extract_name,
unpack_nested_dtype,
)
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
from pandas.core.indexes.extension import ExtensionIndex, inherit_names
Expand Down Expand Up @@ -157,7 +156,6 @@ def wrapped(self, other, sort=False):

>>> pd.interval_range(start=0, end=5)
IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]],
closed='right',
dtype='interval[int64, right]')

It may also be constructed using one of the constructor
Expand Down Expand Up @@ -242,7 +240,6 @@ def _simple_new(cls, array: IntervalArray, name: Label = None):
--------
>>> pd.IntervalIndex.from_breaks([0, 1, 2, 3])
IntervalIndex([(0, 1], (1, 2], (2, 3]],
closed='right',
dtype='interval[int64, right]')
"""
),
Expand Down Expand Up @@ -273,7 +270,6 @@ def from_breaks(
--------
>>> pd.IntervalIndex.from_arrays([0, 1, 2], [1, 2, 3])
IntervalIndex([(0, 1], (1, 2], (2, 3]],
closed='right',
dtype='interval[int64, right]')
"""
),
Expand Down Expand Up @@ -305,7 +301,6 @@ def from_arrays(
--------
>>> pd.IntervalIndex.from_tuples([(0, 1), (1, 2)])
IntervalIndex([(0, 1], (1, 2]],
closed='right',
dtype='interval[int64, right]')
"""
),
Expand Down Expand Up @@ -445,7 +440,6 @@ def is_overlapping(self) -> bool:
>>> index = pd.IntervalIndex.from_tuples([(0, 2), (1, 3), (4, 5)])
>>> index
IntervalIndex([(0, 2], (1, 3], (4, 5]],
closed='right',
dtype='interval[int64, right]')
>>> index.is_overlapping
True
Expand All @@ -455,7 +449,6 @@ def is_overlapping(self) -> bool:
>>> index = pd.interval_range(0, 3, closed='both')
>>> index
IntervalIndex([[0, 1], [1, 2], [2, 3]],
closed='both',
dtype='interval[int64, both]')
>>> index.is_overlapping
True
Expand All @@ -465,7 +458,6 @@ def is_overlapping(self) -> bool:
>>> index = pd.interval_range(0, 3, closed='left')
>>> index
IntervalIndex([[0, 1), [1, 2), [2, 3)],
closed='left',
dtype='interval[int64, left]')
>>> index.is_overlapping
False
Expand Down Expand Up @@ -777,17 +769,11 @@ def _convert_list_indexer(self, keyarr):
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
if not isinstance(dtype, IntervalDtype):
return False
if self.closed != dtype.closed:
return False
common_subtype = find_common_type([self.dtype.subtype, dtype.subtype])
return not is_object_dtype(common_subtype)

def _should_compare(self, other) -> bool:
other = unpack_nested_dtype(other)
if is_object_dtype(other.dtype):
return True
if not self._is_comparable_dtype(other.dtype):
return False
return other.closed == self.closed

# --------------------------------------------------------------------

@cache_readonly
Expand Down Expand Up @@ -911,7 +897,7 @@ def _format_data(self, name=None):
return summary + "," + self._format_space()

def _format_attrs(self):
attrs = [("closed", repr(self.closed))]
attrs = []
if self.name is not None:
attrs.append(("name", default_pprint(self.name)))
attrs.append(("dtype", f"'{self.dtype}'"))
Expand Down Expand Up @@ -1117,7 +1103,6 @@ def interval_range(

>>> pd.interval_range(start=0, end=5)
IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]],
closed='right',
dtype='interval[int64, right]')

Additionally, datetime-like input is also supported.
Expand All @@ -1126,15 +1111,15 @@ def interval_range(
... end=pd.Timestamp('2017-01-04'))
IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03],
(2017-01-03, 2017-01-04]],
closed='right', dtype='interval[datetime64[ns], right]')
dtype='interval[datetime64[ns], right]')

The ``freq`` parameter specifies the frequency between the left and right.
endpoints of the individual intervals within the ``IntervalIndex``. For
numeric ``start`` and ``end``, the frequency must also be numeric.

>>> pd.interval_range(start=0, periods=4, freq=1.5)
IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]],
closed='right', dtype='interval[float64, right]')
dtype='interval[float64, right]')

Similarly, for datetime-like ``start`` and ``end``, the frequency must be
convertible to a DateOffset.
Expand All @@ -1143,22 +1128,21 @@ def interval_range(
... periods=3, freq='MS')
IntervalIndex([(2017-01-01, 2017-02-01], (2017-02-01, 2017-03-01],
(2017-03-01, 2017-04-01]],
closed='right', dtype='interval[datetime64[ns], right]')
dtype='interval[datetime64[ns], right]')

Specify ``start``, ``end``, and ``periods``; the frequency is generated
automatically (linearly spaced).

>>> pd.interval_range(start=0, end=6, periods=4)
IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]],
closed='right',
dtype='interval[float64, right]')

The ``closed`` parameter specifies which endpoints of the individual
intervals within the ``IntervalIndex`` are closed.

>>> pd.interval_range(end=5, periods=4, closed='both')
IntervalIndex([[1, 2], [2, 3], [3, 4], [4, 5]],
closed='both', dtype='interval[int64, both]')
dtype='interval[int64, both]')
"""
start = maybe_box_datetimelike(start)
end = maybe_box_datetimelike(end)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_repr():
expected = (
"<IntervalArray>\n"
"[(0, 1], (1, 2]]\n"
"Length: 2, closed: right, dtype: interval[int64, right]"
"Length: 2, dtype: interval[int64, right]"
)
assert result == expected

Expand Down