Skip to content

Commit f27d70f

Browse files
authored
REF: implement ExtensionIndex._concat_same_dtype, use for IntervalIndex (#31635)
1 parent fcf7258 commit f27d70f

File tree

4 files changed

+16
-49
lines changed

4 files changed

+16
-49
lines changed

pandas/core/indexes/datetimelike.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,7 @@ def wrapper(left, right):
8080
cache=True,
8181
)
8282
@inherit_names(
83-
[
84-
"__iter__",
85-
"mean",
86-
"freq",
87-
"freqstr",
88-
"_ndarray_values",
89-
"asi8",
90-
"_box_values",
91-
"_box_func",
92-
],
83+
["mean", "freq", "freqstr", "asi8", "_box_values", "_box_func"],
9384
DatetimeLikeArrayMixin,
9485
)
9586
class DatetimeIndexOpsMixin(ExtensionIndex):

pandas/core/indexes/extension.py

+12
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ class ExtensionIndex(Index):
196196
Index subclass for indexes backed by ExtensionArray.
197197
"""
198198

199+
# The base class already passes through to _data:
200+
# size, __len__, dtype
201+
199202
_data: ExtensionArray
200203

201204
__eq__ = _make_wrapped_comparison_op("__eq__")
@@ -205,6 +208,9 @@ class ExtensionIndex(Index):
205208
__le__ = _make_wrapped_comparison_op("__le__")
206209
__ge__ = _make_wrapped_comparison_op("__ge__")
207210

211+
# ---------------------------------------------------------------------
212+
# NDarray-Like Methods
213+
208214
def __getitem__(self, key):
209215
result = self._data[key]
210216
if isinstance(result, type(self._data)):
@@ -217,6 +223,8 @@ def __getitem__(self, key):
217223
def __iter__(self):
218224
return self._data.__iter__()
219225

226+
# ---------------------------------------------------------------------
227+
220228
@property
221229
def _ndarray_values(self) -> np.ndarray:
222230
return self._data._ndarray_values
@@ -235,6 +243,10 @@ def repeat(self, repeats, axis=None):
235243
result = self._data.repeat(repeats, axis=axis)
236244
return self._shallow_copy(result)
237245

246+
def _concat_same_dtype(self, to_concat, name):
247+
arr = type(self._data)._concat_same_type(to_concat)
248+
return type(self)._simple_new(arr, name=name)
249+
238250
@Appender(Index.take.__doc__)
239251
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
240252
nv.validate_take(tuple(), kwargs)

pandas/core/indexes/interval.py

+2-35
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,10 @@ def func(intvidx_self, other, sort=False):
183183
)
184184
@inherit_names(["set_closed", "to_tuples"], IntervalArray, wrap=True)
185185
@inherit_names(
186-
[
187-
"__len__",
188-
"__array__",
189-
"overlaps",
190-
"contains",
191-
"size",
192-
"dtype",
193-
"left",
194-
"right",
195-
"length",
196-
],
197-
IntervalArray,
186+
["__array__", "overlaps", "contains", "left", "right", "length"], IntervalArray,
198187
)
199188
@inherit_names(
200-
["is_non_overlapping_monotonic", "mid", "_ndarray_values", "closed"],
201-
IntervalArray,
202-
cache=True,
189+
["is_non_overlapping_monotonic", "mid", "closed"], IntervalArray, cache=True,
203190
)
204191
class IntervalIndex(IntervalMixin, ExtensionIndex):
205192
_typ = "intervalindex"
@@ -943,33 +930,13 @@ def insert(self, loc, item):
943930
new_right = self.right.insert(loc, right_insert)
944931
return self._shallow_copy(new_left, new_right)
945932

946-
def _concat_same_dtype(self, to_concat, name):
947-
"""
948-
assert that we all have the same .closed
949-
we allow a 0-len index here as well
950-
"""
951-
if not len({i.closed for i in to_concat if len(i)}) == 1:
952-
raise ValueError(
953-
"can only append two IntervalIndex objects "
954-
"that are closed on the same side"
955-
)
956-
return super()._concat_same_dtype(to_concat, name)
957-
958933
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
959934
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
960935
result = self._data.take(
961936
indices, axis=axis, allow_fill=allow_fill, fill_value=fill_value, **kwargs
962937
)
963938
return self._shallow_copy(result)
964939

965-
def __getitem__(self, value):
966-
result = self._data[value]
967-
if isinstance(result, IntervalArray):
968-
return self._shallow_copy(result)
969-
else:
970-
# scalar
971-
return result
972-
973940
# --------------------------------------------------------------------
974941
# Rendering Methods
975942
# __repr__ associated methods are based on MultiIndex

pandas/tests/indexes/interval/test_interval.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,7 @@ def test_append(self, closed):
673673
)
674674
tm.assert_index_equal(result, expected)
675675

676-
msg = (
677-
"can only append two IntervalIndex objects that are closed "
678-
"on the same side"
679-
)
676+
msg = "Intervals must all be closed on the same side"
680677
for other_closed in {"left", "right", "both", "neither"} - {closed}:
681678
index_other_closed = IntervalIndex.from_arrays(
682679
[0, 1], [1, 2], closed=other_closed

0 commit comments

Comments
 (0)