Skip to content

Backport PR #31502 on branch 1.0.x (BUG: Fixed IntervalArray[int].shift) #31656

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
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
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Bug fixes

- Plotting tz-aware timeseries no longer gives UserWarning (:issue:`31205`)

**Interval**

- Bug in :meth:`Series.shift` with ``interval`` dtype raising a ``TypeError`` when shifting an interval array of integers or datetimes (:issue:`34195`)

.. ---------------------------------------------------------------------------

Expand Down
28 changes: 28 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pandas.core.dtypes.dtypes import IntervalDtype
from pandas.core.dtypes.generic import (
ABCDatetimeIndex,
ABCExtensionArray,
ABCIndexClass,
ABCInterval,
ABCIntervalIndex,
Expand Down Expand Up @@ -789,6 +790,33 @@ def size(self) -> int:
# Avoid materializing self.values
return self.left.size

def shift(self, periods: int = 1, fill_value: object = None) -> ABCExtensionArray:
if not len(self) or periods == 0:
return self.copy()

if isna(fill_value):
fill_value = self.dtype.na_value

# ExtensionArray.shift doesn't work for two reasons
# 1. IntervalArray.dtype.na_value may not be correct for the dtype.
# 2. IntervalArray._from_sequence only accepts NaN for missing values,
# not other values like NaT

empty_len = min(abs(periods), len(self))
if isna(fill_value):
fill_value = self.left._na_value
empty = IntervalArray.from_breaks([fill_value] * (empty_len + 1))
else:
empty = self._from_sequence([fill_value] * empty_len)

if periods > 0:
a = empty
b = self[:-periods]
else:
a = self[abs(periods) :]
b = empty
return self._concat_same_type([a, b])

def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
"""
Take elements from the IntervalArray.
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ def test_where_raises(self, other):
with pytest.raises(ValueError, match=match):
ser.where([True, False, True], other=other)

def test_shift(self):
# https://github.com/pandas-dev/pandas/issues/31495
a = IntervalArray.from_breaks([1, 2, 3])
result = a.shift()
# int -> float
expected = IntervalArray.from_tuples([(np.nan, np.nan), (1.0, 2.0)])
tm.assert_interval_array_equal(result, expected)

def test_shift_datetime(self):
a = IntervalArray.from_breaks(pd.date_range("2000", periods=4))
result = a.shift(2)
expected = a.take([-1, -1, 0], allow_fill=True)
tm.assert_interval_array_equal(result, expected)

result = a.shift(-1)
expected = a.take([1, 2, -1], allow_fill=True)
tm.assert_interval_array_equal(result, expected)


class TestSetitem:
def test_set_na(self, left_right_dtypes):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ def test_shift_empty_array(self, data, periods):
expected = empty
self.assert_extension_array_equal(result, expected)

def test_shift_zero_copies(self, data):
result = data.shift(0)
assert result is not data

result = data[:0].shift(2)
assert result is not data

def test_shift_fill_value(self, data):
arr = data[:4]
fill_value = data[0]
Expand Down