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

BUG: Handle nonexistent end dates when resampling #59471

Merged
merged 3 commits into from
Aug 21, 2024

Conversation

rob-sil
Copy link
Contributor

@rob-sil rob-sil commented Aug 10, 2024

@rhshadrach rhshadrach added Bug Resample resample method labels Aug 11, 2024
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

expected = Series([len(ts)], index=DatetimeIndex([ts.index[0]], freq="7D"))

result = ts.resample("7D").sum()
print(f"{result=}")
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the print.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, sorry about that!

@@ -615,6 +615,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
- Bug in :meth:`DataFrameGroupby.transform` and :meth:`SeriesGroupby.transform` with a reducer and ``observed=False`` that coerces dtype to float when there are unobserved categories. (:issue:`55326`)
- 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 the date range ended shortly before DST. (:issue:`58380`)
Copy link
Member

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?

@rob-sil rob-sil requested a review from rhshadrach August 11, 2024 19:45
@@ -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")
Copy link
Member

@mroeschke mroeschke Aug 12, 2024

Choose a reason for hiding this comment

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

Should nonexistent be dependent on closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think last here is used as the last element in the series. Instead, last needs to be strictly after all the data points:

pandas/pandas/_libs/lib.pyx

Lines 893 to 894 in 0fadaa9

if values[lenidx - 1] > binner[lenbin - 1]:
raise ValueError("Values falls after last bin")

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:

2024-04-24 00:00:00+02:00    1
2024-04-25 00:00:00+02:00    1
Freq: D, dtype: int64

(I'm not sure why it gives a bin on 2024-04-24.)

@mroeschke mroeschke added the Timezones Timezone data dtype label Aug 12, 2024
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM merge when ready @rhshadrach

@mroeschke mroeschke added this to the 3.0 milestone Aug 19, 2024
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

lgtm

@rhshadrach rhshadrach merged commit 328e79d into pandas-dev:main Aug 21, 2024
51 checks passed
@rhshadrach
Copy link
Member

Thanks @rob-sil!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Resample resample method Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: NonExistentTimeError with resample("7D"), but only when DST day is not part of the date range
3 participants