Skip to content

Commit 8f13b18

Browse files
committed
🤔 fix for [BUG] Error handling timezones #305
1 parent 18da98f commit 8f13b18

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

plotly_resampler/aggregation/plotly_aggregator_parser.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ def to_same_tz(
3838
return None
3939
elif reference_tz is not None:
4040
if ts.tz is not None:
41-
assert ts.tz.__str__() == reference_tz.__str__()
42-
return ts
41+
# compare if these two have the same timezone / offset
42+
print('to same tz', 'ts', ts.tz.__str__(), 'ref', reference_tz.__str__())
43+
try:
44+
assert ts.tz.__str__() == reference_tz.__str__()
45+
except AssertionError:
46+
assert ts.utcoffset() == reference_tz.utcoffset(ts.tz_convert(None))
4347
else: # localize -> time remains the same
4448
return ts.tz_localize(reference_tz)
4549
elif reference_tz is None and ts.tz is not None:
@@ -78,7 +82,8 @@ def get_start_end_indices(hf_trace_data, axis_type, start, end) -> Tuple[int, in
7882
# convert start & end to the same timezone
7983
if isinstance(hf_trace_data["x"], pd.DatetimeIndex):
8084
tz = hf_trace_data["x"].tz
81-
assert start.tz == end.tz
85+
# print(start.tz.__str__(), end.tz.__str__())
86+
assert start.tz.__str__() == end.tz.__str__()
8287
start = PlotlyAggregatorParser.to_same_tz(start, tz)
8388
end = PlotlyAggregatorParser.to_same_tz(end, tz)
8489

tests/test_figure_resampler.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,53 @@ def test_tz_xaxis_range():
754754
assert len(out[2]["x"]) == 2000
755755

756756

757+
def test_compare_tz_with_fixed_offset():
758+
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
759+
fig = FigureResampler()
760+
761+
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="H")
762+
x = x.tz_localize("Asia/Taipei")
763+
y = np.random.randn(len(x))
764+
765+
fig.add_trace(
766+
go.Scattergl(x=x, y=y, name="demo", mode="lines+markers"),
767+
max_n_samples=int(len(x) * 0.2),
768+
)
769+
770+
relayout_data = {
771+
"xaxis.range[0]": "2024-04-27T08:00:00+08:00",
772+
"xaxis.range[1]": "2024-05-04T17:15:39.491031+08:00",
773+
}
774+
775+
fig.construct_update_data_patch(relayout_data)
776+
777+
778+
def test_compare_tz_with_fixed_offset_2():
779+
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
780+
fig = FigureResampler()
781+
782+
x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="H")
783+
x = x.tz_localize("UTC")
784+
x = x.tz_convert("Canada/Pacific")
785+
y = np.random.randn(len(x))
786+
787+
fig.add_trace(
788+
go.Scattergl(x=x, y=y, name="demo", mode="lines+markers"),
789+
max_n_samples=int(len(x) * 0.2),
790+
)
791+
792+
relayout_data = {
793+
"xaxis.range[0]": pd.Timestamp("2024-03-01T00:00:00").tz_localize(
794+
"Canada/Pacific"
795+
),
796+
"xaxis.range[1]": pd.Timestamp("2024-03-31T00:00:00").tz_localize(
797+
"Canada/Pacific"
798+
),
799+
}
800+
801+
fig.construct_update_data_patch(relayout_data)
802+
803+
757804
def test_datetime_hf_x_no_index():
758805
df = pd.DataFrame(
759806
{"timestamp": pd.date_range("2020-01-01", "2020-01-02", freq="1s")}
@@ -1009,7 +1056,7 @@ def test_time_tz_slicing_different_timestamp():
10091056
cs = [
10101057
dr,
10111058
dr.tz_localize(None).tz_localize("Europe/Amsterdam"),
1012-
dr.tz_convert("Europe/Brussels"),
1059+
dr.tz_convert("Europe/Lisbon"),
10131060
dr.tz_convert("Australia/Perth"),
10141061
dr.tz_convert("Australia/Canberra"),
10151062
]
@@ -1027,6 +1074,26 @@ def test_time_tz_slicing_different_timestamp():
10271074
hf_data_dict, hf_data_dict["axis_type"], t_start, t_stop
10281075
)
10291076

1077+
# THESE have the same timezone offset -> no AssertionError should be raised
1078+
cs = [
1079+
dr.tz_localize(None).tz_localize("Europe/Amsterdam"),
1080+
dr.tz_convert("Europe/Brussels"),
1081+
dr.tz_convert("Europe/Oslo"),
1082+
dr.tz_convert("Europe/Paris"),
1083+
dr.tz_convert("Europe/Rome"),
1084+
]
1085+
1086+
for i, s in enumerate(cs):
1087+
t_start, t_stop = sorted(s.iloc[np.random.randint(0, n, 2)].index)
1088+
t_start = t_start.tz_convert(cs[(i + 1) % len(cs)].index.tz)
1089+
t_stop = t_stop.tz_convert(cs[(i + 1) % len(cs)].index.tz)
1090+
1091+
hf_data_dict = construct_hf_data_dict(s.index, s.values)
1092+
start_idx, end_idx = PlotlyAggregatorParser.get_start_end_indices(
1093+
hf_data_dict, hf_data_dict["axis_type"], t_start, t_stop
1094+
)
1095+
1096+
10301097

10311098
def test_different_tz_no_tz_series_slicing():
10321099
n = 60 * 60 * 24 * 3

0 commit comments

Comments
 (0)