diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b143ff0aa9c02..2f2fc453f02b8 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -80,16 +80,7 @@ def wrapper(left, right): cache=True, ) @inherit_names( - [ - "__iter__", - "mean", - "freq", - "freqstr", - "_ndarray_values", - "asi8", - "_box_values", - "_box_func", - ], + ["mean", "freq", "freqstr", "asi8", "_box_values", "_box_func"], DatetimeLikeArrayMixin, ) class DatetimeIndexOpsMixin(ExtensionIndex): diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 66b551f654bf1..04b4b275bf90a 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -196,6 +196,9 @@ class ExtensionIndex(Index): Index subclass for indexes backed by ExtensionArray. """ + # The base class already passes through to _data: + # size, __len__, dtype + _data: ExtensionArray __eq__ = _make_wrapped_comparison_op("__eq__") @@ -205,6 +208,9 @@ class ExtensionIndex(Index): __le__ = _make_wrapped_comparison_op("__le__") __ge__ = _make_wrapped_comparison_op("__ge__") + # --------------------------------------------------------------------- + # NDarray-Like Methods + def __getitem__(self, key): result = self._data[key] if isinstance(result, type(self._data)): @@ -217,6 +223,8 @@ def __getitem__(self, key): def __iter__(self): return self._data.__iter__() + # --------------------------------------------------------------------- + @property def _ndarray_values(self) -> np.ndarray: return self._data._ndarray_values @@ -235,6 +243,10 @@ def repeat(self, repeats, axis=None): result = self._data.repeat(repeats, axis=axis) return self._shallow_copy(result) + def _concat_same_dtype(self, to_concat, name): + arr = type(self._data)._concat_same_type(to_concat) + return type(self)._simple_new(arr, name=name) + @Appender(Index.take.__doc__) def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): nv.validate_take(tuple(), kwargs) diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 03fb8db2e1e1e..f77785a29b262 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -183,23 +183,10 @@ def func(intvidx_self, other, sort=False): ) @inherit_names(["set_closed", "to_tuples"], IntervalArray, wrap=True) @inherit_names( - [ - "__len__", - "__array__", - "overlaps", - "contains", - "size", - "dtype", - "left", - "right", - "length", - ], - IntervalArray, + ["__array__", "overlaps", "contains", "left", "right", "length"], IntervalArray, ) @inherit_names( - ["is_non_overlapping_monotonic", "mid", "_ndarray_values", "closed"], - IntervalArray, - cache=True, + ["is_non_overlapping_monotonic", "mid", "closed"], IntervalArray, cache=True, ) class IntervalIndex(IntervalMixin, ExtensionIndex): _typ = "intervalindex" @@ -943,18 +930,6 @@ def insert(self, loc, item): new_right = self.right.insert(loc, right_insert) return self._shallow_copy(new_left, new_right) - def _concat_same_dtype(self, to_concat, name): - """ - assert that we all have the same .closed - we allow a 0-len index here as well - """ - if not len({i.closed for i in to_concat if len(i)}) == 1: - raise ValueError( - "can only append two IntervalIndex objects " - "that are closed on the same side" - ) - return super()._concat_same_dtype(to_concat, name) - @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): result = self._data.take( @@ -962,14 +937,6 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): ) return self._shallow_copy(result) - def __getitem__(self, value): - result = self._data[value] - if isinstance(result, IntervalArray): - return self._shallow_copy(result) - else: - # scalar - return result - # -------------------------------------------------------------------- # Rendering Methods # __repr__ associated methods are based on MultiIndex diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 47a0ba7fe0f21..d010060880703 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -673,10 +673,7 @@ def test_append(self, closed): ) tm.assert_index_equal(result, expected) - msg = ( - "can only append two IntervalIndex objects that are closed " - "on the same side" - ) + msg = "Intervals must all be closed on the same side" for other_closed in {"left", "right", "both", "neither"} - {closed}: index_other_closed = IntervalIndex.from_arrays( [0, 1], [1, 2], closed=other_closed