Skip to content

Commit f38a5c1

Browse files
committed
🤔 fix for [BUG] Error handling timezones #305
1 parent 74d4c2b commit f38a5c1

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

plotly_resampler/aggregation/plotly_aggregator_parser.py

+8-3
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

+68-1
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,53 @@ def test_tz_xaxis_range():
758758
assert len(out[2]["x"]) == 2000
759759

760760

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

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

10351102
def test_different_tz_no_tz_series_slicing():
10361103
n = 60 * 60 * 24 * 3

0 commit comments

Comments
 (0)