Skip to content
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

Fix BUG: Cannot shift Intervals that are not closed='right' (the default) #60407

Merged
merged 1 commit into from
Nov 25, 2024
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
4 changes: 3 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,9 @@ def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray:
from pandas import Index

fill_value = Index(self._left, copy=False)._na_value
empty = IntervalArray.from_breaks([fill_value] * (empty_len + 1))
empty = IntervalArray.from_breaks(
[fill_value] * (empty_len + 1), closed=self.closed
)
else:
empty = self._from_sequence([fill_value] * empty_len, dtype=self.dtype)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,12 @@ def test_shift_with_offsets_freq_empty(self):
df_shifted = DataFrame(index=shifted_dates)
result = df.shift(freq=offset)
tm.assert_frame_equal(result, df_shifted)

def test_series_shift_interval_preserves_closed(self):
# GH#60389
ser = Series(
[pd.Interval(1, 2, closed="right"), pd.Interval(2, 3, closed="right")]
)
result = ser.shift(1)
expected = Series([np.nan, pd.Interval(1, 2, closed="right")])
tm.assert_series_equal(result, expected)