Skip to content

Commit

Permalink
Fix BUG: Cannot shift Intervals that are not closed='right' (the defa…
Browse files Browse the repository at this point in the history
…ult) (#60407)

first
  • Loading branch information
lfffkh authored Nov 25, 2024
1 parent e78df6f commit cbd90ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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)

0 comments on commit cbd90ba

Please sign in to comment.