Skip to content

Commit 5108824

Browse files
committed
📌 bug: Fix timezone handling for DST in PlotlyAggregatorParser and update tests
1 parent bb5c367 commit 5108824

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

plotly_resampler/aggregation/plotly_aggregator_parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ def get_start_end_indices(hf_trace_data, axis_type, start, end) -> Tuple[int, in
8282
# convert start & end to the same timezone
8383
if isinstance(hf_trace_data["x"], pd.DatetimeIndex):
8484
tz = hf_trace_data["x"].tz
85-
# print(start.tz.__str__(), end.tz.__str__())
86-
assert start.tz.__str__() == end.tz.__str__()
85+
try:
86+
assert start.tz.__str__() == end.tz.__str__()
87+
except (TypeError, AssertionError):
88+
# This fix is needed for DST (when the timezone is not fixed)
89+
assert start.tz_localize(None) == start.tz_convert(tz).tz_localize(
90+
None
91+
)
92+
assert end.tz_localize(None) == end.tz_convert(tz).tz_localize(None)
93+
8794
start = PlotlyAggregatorParser.to_same_tz(start, tz)
8895
end = PlotlyAggregatorParser.to_same_tz(end, tz)
8996

tests/test_figure_resampler.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def test_compare_tz_with_fixed_offset():
758758
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
759759
fig = FigureResampler()
760760

761-
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="H")
761+
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="h")
762762
x = x.tz_localize("Asia/Taipei")
763763
y = np.random.randn(len(x))
764764

@@ -779,7 +779,7 @@ def test_compare_tz_with_fixed_offset_2():
779779
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
780780
fig = FigureResampler()
781781

782-
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="H")
782+
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="h")
783783
x = x.tz_localize("UTC")
784784
x = x.tz_convert("Canada/Pacific")
785785
y = np.random.randn(len(x))
@@ -801,6 +801,27 @@ def test_compare_tz_with_fixed_offset_2():
801801
fig.construct_update_data_patch(relayout_data)
802802

803803

804+
def test_relayout_tz_DST():
805+
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
806+
fig = FigureResampler()
807+
808+
x = pd.date_range(
809+
"2024-09-27 17:00:00", "2024-12-11 16:00:00", tz="US/Pacific", freq="1h"
810+
)
811+
y = np.random.randn(len(x))
812+
fig.add_trace(
813+
go.Scattergl(x=x, y=y, name="demo", mode="lines+markers"),
814+
max_n_samples=int(len(x) * 0.2),
815+
)
816+
817+
relayout_data = {
818+
"xaxis.range[0]": "2024-09-27T17:00:00-07:00",
819+
"xaxis.range[1]": "2024-12-12T15:59:00-08:00",
820+
}
821+
822+
fig.construct_update_data_patch(relayout_data)
823+
824+
804825
def test_datetime_hf_x_no_index():
805826
df = pd.DataFrame(
806827
{"timestamp": pd.date_range("2020-01-01", "2020-01-02", freq="1s")}
@@ -836,8 +857,8 @@ def test_multiple_timezones_in_single_x_index__datetimes_and_timestamps():
836857
# TODO: can be improved with pytest parametrize
837858
y = np.arange(20)
838859

839-
index1 = pd.date_range("2018-01-01", periods=10, freq="H", tz="US/Eastern")
840-
index2 = pd.date_range("2018-01-02", periods=10, freq="H", tz="Asia/Dubai")
860+
index1 = pd.date_range("2018-01-01", periods=10, freq="h", tz="US/Eastern")
861+
index2 = pd.date_range("2018-01-02", periods=10, freq="h", tz="Asia/Dubai")
841862
index_timestamps = index1.append(index2)
842863
assert all(isinstance(x, pd.Timestamp) for x in index_timestamps)
843864
index1_datetimes = pd.Index([x.to_pydatetime() for x in index1])
@@ -1158,7 +1179,7 @@ def test_multiple_tz_no_tz_series_slicing():
11581179

11591180
# Now the assumption cannot be made that s has the same time-zone as the
11601181
# timestamps -> AssertionError will be raised.
1161-
with pytest.raises(AssertionError):
1182+
with pytest.raises((TypeError, AssertionError)):
11621183
hf_data_dict = construct_hf_data_dict(s.tz_localize(None).index, s.values)
11631184
PlotlyAggregatorParser.get_start_end_indices(
11641185
hf_data_dict, hf_data_dict["axis_type"], t_start, t_stop

0 commit comments

Comments
 (0)