Skip to content

Commit ffc6fee

Browse files
authored
BUG: Series.resample failing for non-nanosecond unit that's out of bounds for nano reso (#62955)
1 parent 4075fea commit ffc6fee

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ Groupby/resample/rolling
11811181
- 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`)
11821182
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
11831183
- Bug in :meth:`Series.resample` could raise when the date range ended shortly before a non-existent time. (:issue:`58380`)
1184+
- Bug in :meth:`Series.resample` raising error when resampling non-nanosecond resolutions out of bounds for nanosecond precision (:issue:`57427`)
11841185

11851186
Reshaping
11861187
^^^^^^^^^

pandas/_libs/tslibs/offsets.pyx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,18 +5688,27 @@ def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetim
56885688
cdef:
56895689
int year, month, day
56905690
int days_in_month, dy
5691+
npy_datetimestruct dts
5692+
5693+
if isinstance(stamp, _Timestamp):
5694+
creso = (<_Timestamp>stamp)._creso
5695+
val = (<_Timestamp>stamp)._value
5696+
pandas_datetime_to_datetimestruct(val, creso, &dts)
5697+
else:
5698+
# Plain datetime/date
5699+
pydate_to_dtstruct(stamp, &dts)
56915700

5692-
dy = (stamp.month + months) // 12
5693-
month = (stamp.month + months) % 12
5701+
dy = (dts.month + months) // 12
5702+
month = (dts.month + months) % 12
56945703

56955704
if month == 0:
56965705
month = 12
56975706
dy -= 1
5698-
year = stamp.year + dy
5707+
year = dts.year + dy
56995708

57005709
if day_opt is None:
57015710
days_in_month = get_days_in_month(year, month)
5702-
day = min(stamp.day, days_in_month)
5711+
day = min(dts.day, days_in_month)
57035712
elif day_opt == "start":
57045713
day = 1
57055714
elif day_opt == "end":

pandas/tests/resample/test_datetime_index.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,17 @@ def test_arrow_timestamp_resample_keep_index_name():
21712171
tm.assert_series_equal(result, expected)
21722172

21732173

2174+
def test_resample_unit_second_large_years():
2175+
# GH#57427
2176+
index = DatetimeIndex(
2177+
date_range(start=Timestamp("1950-01-01"), periods=10, freq="1000YS", unit="s")
2178+
)
2179+
ser = Series(1, index=index)
2180+
result = ser.resample("2000YS").sum()
2181+
expected = Series(2, index=index[::2])
2182+
tm.assert_series_equal(result, expected)
2183+
2184+
21742185
@pytest.mark.parametrize("freq", ["1A", "2A-MAR"])
21752186
def test_resample_A_raises(freq):
21762187
msg = f"Invalid frequency: {freq[1:]}"

0 commit comments

Comments
 (0)