Skip to content

Commit 41ddc27

Browse files
jbrockmendelroberthdevries
authored andcommitted
REF: simplify IntervalIndex/IntervalArray _shallow_copy (pandas-dev#32247)
1 parent 3e8415f commit 41ddc27

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

pandas/core/arrays/interval.py

+7-32
Original file line numberDiff line numberDiff line change
@@ -725,45 +725,18 @@ def _concat_same_type(cls, to_concat):
725725
right = np.concatenate([interval.right for interval in to_concat])
726726
return cls._simple_new(left, right, closed=closed, copy=False)
727727

728-
def _shallow_copy(self, left=None, right=None, closed=None):
728+
def _shallow_copy(self, left, right):
729729
"""
730730
Return a new IntervalArray with the replacement attributes
731731
732732
Parameters
733733
----------
734-
left : array-like
734+
left : Index
735735
Values to be used for the left-side of the intervals.
736-
If None, the existing left and right values will be used.
737-
738-
right : array-like
736+
right : Index
739737
Values to be used for the right-side of the intervals.
740-
If None and left is IntervalArray-like, the left and right
741-
of the IntervalArray-like will be used.
742-
743-
closed : {'left', 'right', 'both', 'neither'}, optional
744-
Whether the intervals are closed on the left-side, right-side, both
745-
or neither. If None, the existing closed will be used.
746738
"""
747-
if left is None:
748-
749-
# no values passed
750-
left, right = self.left, self.right
751-
752-
elif right is None:
753-
754-
# only single value passed, could be an IntervalArray
755-
# or array of Intervals
756-
if not isinstance(left, (type(self), ABCIntervalIndex)):
757-
left = type(self)(left)
758-
759-
left, right = left.left, left.right
760-
else:
761-
762-
# both left and right are values
763-
pass
764-
765-
closed = closed or self.closed
766-
return self._simple_new(left, right, closed=closed, verify_integrity=False)
739+
return self._simple_new(left, right, closed=self.closed, verify_integrity=False)
767740

768741
def copy(self):
769742
"""
@@ -1035,7 +1008,9 @@ def set_closed(self, closed):
10351008
msg = f"invalid option for 'closed': {closed}"
10361009
raise ValueError(msg)
10371010

1038-
return self._shallow_copy(closed=closed)
1011+
return type(self)._simple_new(
1012+
left=self.left, right=self.right, closed=closed, verify_integrity=False
1013+
)
10391014

10401015
@property
10411016
def length(self):

pandas/core/indexes/interval.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,12 @@ def from_tuples(
333333
# --------------------------------------------------------------------
334334

335335
@Appender(Index._shallow_copy.__doc__)
336-
def _shallow_copy(self, left=None, right=None, **kwargs):
337-
result = self._data._shallow_copy(left=left, right=right)
336+
def _shallow_copy(self, values=None, **kwargs):
337+
if values is None:
338+
values = self._data
338339
attributes = self._get_attributes_dict()
339340
attributes.update(kwargs)
340-
return self._simple_new(result, **attributes)
341+
return self._simple_new(values, **attributes)
341342

342343
@cache_readonly
343344
def _isnan(self):
@@ -407,7 +408,7 @@ def astype(self, dtype, copy=True):
407408
with rewrite_exception("IntervalArray", type(self).__name__):
408409
new_values = self.values.astype(dtype, copy=copy)
409410
if is_interval_dtype(new_values):
410-
return self._shallow_copy(new_values.left, new_values.right)
411+
return self._shallow_copy(new_values)
411412
return Index.astype(self, dtype, copy=copy)
412413

413414
@property
@@ -881,7 +882,8 @@ def where(self, cond, other=None):
881882
if other is None:
882883
other = self._na_value
883884
values = np.where(cond, self.values, other)
884-
return self._shallow_copy(values)
885+
result = IntervalArray(values)
886+
return self._shallow_copy(result)
885887

886888
def delete(self, loc):
887889
"""
@@ -893,7 +895,8 @@ def delete(self, loc):
893895
"""
894896
new_left = self.left.delete(loc)
895897
new_right = self.right.delete(loc)
896-
return self._shallow_copy(new_left, new_right)
898+
result = self._data._shallow_copy(new_left, new_right)
899+
return self._shallow_copy(result)
897900

898901
def insert(self, loc, item):
899902
"""
@@ -927,7 +930,8 @@ def insert(self, loc, item):
927930

928931
new_left = self.left.insert(loc, left_insert)
929932
new_right = self.right.insert(loc, right_insert)
930-
return self._shallow_copy(new_left, new_right)
933+
result = self._data._shallow_copy(new_left, new_right)
934+
return self._shallow_copy(result)
931935

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

0 commit comments

Comments
 (0)