diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index b2919b45fd6a7..cae9fa949f711 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -201,7 +201,14 @@ def func(intvidx_self, other, sort=False): ) @accessor.delegate_names( delegate=IntervalArray, - accessors=["__array__", "overlaps", "contains", "__len__", "set_closed"], + accessors=[ + "__array__", + "overlaps", + "contains", + "__len__", + "set_closed", + "to_tuples", + ], typ="method", overwrite=True, ) @@ -393,25 +400,6 @@ def __contains__(self, key) -> bool: except KeyError: return False - @Appender( - _interval_shared_docs["to_tuples"] - % dict( - return_type="Index", - examples=""" - Examples - -------- - >>> idx = pd.IntervalIndex.from_arrays([0, np.nan, 2], [1, np.nan, 3]) - >>> idx.to_tuples() - Index([(0.0, 1.0), (nan, nan), (2.0, 3.0)], dtype='object') - >>> idx.to_tuples(na_tuple=False) - Index([(0.0, 1.0), nan, (2.0, 3.0)], dtype='object') - """, - ) - ) - def to_tuples(self, na_tuple=True): - tuples = self._data.to_tuples(na_tuple=na_tuple) - return Index(tuples) - @cache_readonly def _multiindex(self): return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"]) @@ -1004,8 +992,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): result = self._data.take( indices, axis=axis, allow_fill=allow_fill, fill_value=fill_value, **kwargs ) - attributes = self._get_attributes_dict() - return self._simple_new(result, **attributes) + return self._shallow_copy(result) def __getitem__(self, value): result = self._data[value] @@ -1206,7 +1193,9 @@ def _delegate_method(self, name, *args, **kwargs): res = method(*args, **kwargs) if is_scalar(res) or name in self._raw_inherit: return res - return type(self)(res, name=self.name) + if isinstance(res, IntervalArray): + return type(self)._simple_new(res, name=self.name) + return Index(res) IntervalIndex._add_logical_methods_disabled()