Skip to content

REF: simplify IntervalIndex/IntervalArray _shallow_copy #32247

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 4 commits into from
Feb 26, 2020
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
39 changes: 7 additions & 32 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,45 +725,18 @@ def _concat_same_type(cls, to_concat):
right = np.concatenate([interval.right for interval in to_concat])
return cls._simple_new(left, right, closed=closed, copy=False)

def _shallow_copy(self, left=None, right=None, closed=None):
def _shallow_copy(self, left, right):
"""
Return a new IntervalArray with the replacement attributes

Parameters
----------
left : array-like
left : Index
Values to be used for the left-side of the intervals.
If None, the existing left and right values will be used.

right : array-like
right : Index
Values to be used for the right-side of the intervals.
If None and left is IntervalArray-like, the left and right
of the IntervalArray-like will be used.

closed : {'left', 'right', 'both', 'neither'}, optional
Whether the intervals are closed on the left-side, right-side, both
or neither. If None, the existing closed will be used.
"""
if left is None:

# no values passed
left, right = self.left, self.right

elif right is None:

# only single value passed, could be an IntervalArray
# or array of Intervals
if not isinstance(left, (type(self), ABCIntervalIndex)):
left = type(self)(left)

left, right = left.left, left.right
else:

# both left and right are values
pass

closed = closed or self.closed
return self._simple_new(left, right, closed=closed, verify_integrity=False)
return self._simple_new(left, right, closed=self.closed, verify_integrity=False)

def copy(self):
"""
Expand Down Expand Up @@ -1035,7 +1008,9 @@ def set_closed(self, closed):
msg = f"invalid option for 'closed': {closed}"
raise ValueError(msg)

return self._shallow_copy(closed=closed)
return type(self)._simple_new(
left=self.left, right=self.right, closed=closed, verify_integrity=False
)

@property
def length(self):
Expand Down
18 changes: 11 additions & 7 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ def from_tuples(
# --------------------------------------------------------------------

@Appender(Index._shallow_copy.__doc__)
def _shallow_copy(self, left=None, right=None, **kwargs):
result = self._data._shallow_copy(left=left, right=right)
def _shallow_copy(self, values=None, **kwargs):
if values is None:
values = self._data
attributes = self._get_attributes_dict()
attributes.update(kwargs)
return self._simple_new(result, **attributes)
return self._simple_new(values, **attributes)

@cache_readonly
def _isnan(self):
Expand Down Expand Up @@ -407,7 +408,7 @@ def astype(self, dtype, copy=True):
with rewrite_exception("IntervalArray", type(self).__name__):
new_values = self.values.astype(dtype, copy=copy)
if is_interval_dtype(new_values):
return self._shallow_copy(new_values.left, new_values.right)
return self._shallow_copy(new_values)
return Index.astype(self, dtype, copy=copy)

@property
Expand Down Expand Up @@ -881,7 +882,8 @@ def where(self, cond, other=None):
if other is None:
other = self._na_value
values = np.where(cond, self.values, other)
return self._shallow_copy(values)
result = IntervalArray(values)
return self._shallow_copy(result)

def delete(self, loc):
"""
Expand All @@ -893,7 +895,8 @@ def delete(self, loc):
"""
new_left = self.left.delete(loc)
new_right = self.right.delete(loc)
return self._shallow_copy(new_left, new_right)
result = self._data._shallow_copy(new_left, new_right)
return self._shallow_copy(result)

def insert(self, loc, item):
"""
Expand Down Expand Up @@ -927,7 +930,8 @@ def insert(self, loc, item):

new_left = self.left.insert(loc, left_insert)
new_right = self.right.insert(loc, right_insert)
return self._shallow_copy(new_left, new_right)
result = self._data._shallow_copy(new_left, new_right)
return self._shallow_copy(result)

@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
Expand Down