Skip to content

Commit

Permalink
REF: delegate more IntervalIndex methods (#30626)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jschendel committed Jan 3, 2020
1 parent 09e4b78 commit dd0d353
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit dd0d353

Please sign in to comment.