-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Handle nonexistent end dates when resampling #59471
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2466,7 +2466,7 @@ def _get_timestamp_range_edges( | |||||
) | ||||||
if isinstance(freq, Day): | ||||||
first = first.tz_localize(index_tz) | ||||||
last = last.tz_localize(index_tz) | ||||||
last = last.tz_localize(index_tz, nonexistent="shift_forward") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think Lines 893 to 894 in 0fadaa9
Shifting backwards should raise when running near a nonexistent hour, in the rare case where the nanosecond before is in the index: import pandas as pd
almost_a_day = pd.Timedelta(days=1) - pd.Timedelta(nanoseconds=1)
timestamp = pd.to_datetime("2024-04-25").tz_localize("Africa/Cairo")
ts = pd.Series(
1,
[timestamp, timestamp + almost_a_day],
)
ts.resample("1D", closed="right").sum() However, shifting forward runs with the following output:
(I'm not sure why it gives a bin on 2024-04-24.) |
||||||
else: | ||||||
first = first.normalize() | ||||||
last = last.normalize() | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -958,6 +958,20 @@ def _create_series(values, timestamps, freq="D"): | |
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_resample_dst_midnight_last_nonexistent(): | ||
# GH 58380 | ||
ts = Series( | ||
1, | ||
date_range("2024-04-19", "2024-04-20", tz="Africa/Cairo", freq="15min"), | ||
) | ||
|
||
expected = Series([len(ts)], index=DatetimeIndex([ts.index[0]], freq="7D")) | ||
|
||
result = ts.resample("7D").sum() | ||
print(f"{result=}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the print. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, sorry about that! |
||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_resample_daily_anchored(unit): | ||
rng = date_range("1/1/2000 0:00:00", periods=10000, freq="min").as_unit(unit) | ||
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not just before DST, but more generally before a non-existent time, right?