Skip to content

Fixes NonExistentTimeError when resampling weekly #54609

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import warnings

import numpy as np
from pytz import NonExistentTimeError

from pandas._libs import lib
from pandas._libs.tslibs import (
Expand Down Expand Up @@ -2494,6 +2495,17 @@ def _get_timestamp_range_edges(
first = first.tz_localize(index_tz)
last = last.tz_localize(index_tz)
else:
# Added to handle non-existent times when localizing to
# time-zones with negative time-zone difference
try:
first = first.tz_localize(None)
except NonExistentTimeError:
first = first.tz_localize(None, nonexistent="shift_forward")

try:
last = last.tz_localize(None)
except NonExistentTimeError:
last = last.tz_localize(None, nonexistent="shift_forward")
if isinstance(origin, Timestamp):
first = origin

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from textwrap import dedent

import numpy as np
Expand Down Expand Up @@ -696,3 +697,19 @@ def test_groupby_resample_kind(kind):
)
expected = Series([1, 3, 2, 4], index=expected_index, name="value")
tm.assert_series_equal(result, expected)


def test_resample_dataframe_with_negative_timezone_difference():
df = DataFrame({"ts": [datetime(2005, 10, 9, 21)], "values": [10.0]})
df["ts"] = df["ts"].dt.tz_localize("Chile/Continental")
result = df.resample("W-Mon", on="ts", closed="left", label="left").sum()
expected = DataFrame(
{
"values": {
Timestamp("2005-10-03 00:00:00-0400", tz="Chile/Continental"): 10.0
}
}
)
expected.index.name = "ts"
expected.index.freq = "W-Mon"
tm.assert_frame_equal(result, expected)