Skip to content
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ Groupby/resample/rolling
- Bug in :meth:`Rolling.apply` for ``method="table"`` where column order was not being respected due to the columns getting sorted by default. (:issue:`59666`)
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
- Bug in :meth:`Series.resample` could raise when the date range ended shortly before a non-existent time. (:issue:`58380`)
- Bug in :meth:`Series.resample` raising error when resampling non-nanosecond resolutions out of bounds for nanosecond precision (:issue:`57427`)

Reshaping
^^^^^^^^^
Expand Down
17 changes: 13 additions & 4 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5688,18 +5688,27 @@ def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetim
cdef:
int year, month, day
int days_in_month, dy
npy_datetimestruct dts

if isinstance(stamp, _Timestamp):
creso = (<_Timestamp>stamp)._creso
val = (<_Timestamp>stamp)._value
pandas_datetime_to_datetimestruct(val, creso, &dts)
else:
# Plain datetime/date
pydate_to_dtstruct(stamp, &dts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this by pydatetime_to_dtstruct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about this too but we seem to be using the date version a little earlier in the callstsack as well

I think we actually never get here with a real datetime

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess datetime subclasses date, so this should be safe.


dy = (stamp.month + months) // 12
month = (stamp.month + months) % 12
dy = (dts.month + months) // 12
month = (dts.month + months) % 12

if month == 0:
month = 12
dy -= 1
year = stamp.year + dy
year = dts.year + dy

if day_opt is None:
days_in_month = get_days_in_month(year, month)
day = min(stamp.day, days_in_month)
day = min(dts.day, days_in_month)
elif day_opt == "start":
day = 1
elif day_opt == "end":
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,17 @@ def test_arrow_timestamp_resample_keep_index_name():
tm.assert_series_equal(result, expected)


def test_resample_unit_second_large_years():
# GH#57427
index = DatetimeIndex(
date_range(start=Timestamp("1950-01-01"), periods=10, freq="1000YS", unit="s")
)
ser = Series(1, index=index)
result = ser.resample("2000YS").sum()
expected = Series(2, index=index[::2])
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("freq", ["1A", "2A-MAR"])
def test_resample_A_raises(freq):
msg = f"Invalid frequency: {freq[1:]}"
Expand Down
Loading