Skip to content

Commit

Permalink
🤔 fix for [BUG] Error handling timezones #305
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvdd committed Sep 9, 2024
1 parent 74d4c2b commit f38a5c1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
11 changes: 8 additions & 3 deletions plotly_resampler/aggregation/plotly_aggregator_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ def to_same_tz(
return None
elif reference_tz is not None:
if ts.tz is not None:
assert ts.tz.__str__() == reference_tz.__str__()
return ts
# compare if these two have the same timezone / offset
print('to same tz', 'ts', ts.tz.__str__(), 'ref', reference_tz.__str__())
try:
assert ts.tz.__str__() == reference_tz.__str__()
except AssertionError:
assert ts.utcoffset() == reference_tz.utcoffset(ts.tz_convert(None))
else: # localize -> time remains the same
return ts.tz_localize(reference_tz)
elif reference_tz is None and ts.tz is not None:
Expand Down Expand Up @@ -78,7 +82,8 @@ def get_start_end_indices(hf_trace_data, axis_type, start, end) -> Tuple[int, in
# convert start & end to the same timezone
if isinstance(hf_trace_data["x"], pd.DatetimeIndex):
tz = hf_trace_data["x"].tz
assert start.tz == end.tz
# print(start.tz.__str__(), end.tz.__str__())
assert start.tz.__str__() == end.tz.__str__()
start = PlotlyAggregatorParser.to_same_tz(start, tz)
end = PlotlyAggregatorParser.to_same_tz(end, tz)

Expand Down
69 changes: 68 additions & 1 deletion tests/test_figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,53 @@ def test_tz_xaxis_range():
assert len(out[2]["x"]) == 2000


def test_compare_tz_with_fixed_offset():
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
fig = FigureResampler()

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

fig.add_trace(
go.Scattergl(x=x, y=y, name="demo", mode="lines+markers"),
max_n_samples=int(len(x) * 0.2),
)

relayout_data = {
"xaxis.range[0]": "2024-04-27T08:00:00+08:00",
"xaxis.range[1]": "2024-05-04T17:15:39.491031+08:00",
}

fig.construct_update_data_patch(relayout_data)


def test_compare_tz_with_fixed_offset_2():
# related: https://github.com/predict-idlab/plotly-resampler/issues/305
fig = FigureResampler()

x = pd.date_range("2024-04-01T00:00:00", "2025-01-01T00:00:00", freq="H")
x = x.tz_localize("UTC")
x = x.tz_convert("Canada/Pacific")
y = np.random.randn(len(x))

fig.add_trace(
go.Scattergl(x=x, y=y, name="demo", mode="lines+markers"),
max_n_samples=int(len(x) * 0.2),
)

relayout_data = {
"xaxis.range[0]": pd.Timestamp("2024-03-01T00:00:00").tz_localize(
"Canada/Pacific"
),
"xaxis.range[1]": pd.Timestamp("2024-03-31T00:00:00").tz_localize(
"Canada/Pacific"
),
}

fig.construct_update_data_patch(relayout_data)


def test_datetime_hf_x_no_index():
df = pd.DataFrame(
{"timestamp": pd.date_range("2020-01-01", "2020-01-02", freq="1s")}
Expand Down Expand Up @@ -1013,7 +1060,7 @@ def test_time_tz_slicing_different_timestamp():
cs = [
dr,
dr.tz_localize(None).tz_localize("Europe/Amsterdam"),
dr.tz_convert("Europe/Brussels"),
dr.tz_convert("Europe/Lisbon"),
dr.tz_convert("Australia/Perth"),
dr.tz_convert("Australia/Canberra"),
]
Expand All @@ -1031,6 +1078,26 @@ def test_time_tz_slicing_different_timestamp():
hf_data_dict, hf_data_dict["axis_type"], t_start, t_stop
)

# THESE have the same timezone offset -> no AssertionError should be raised
cs = [
dr.tz_localize(None).tz_localize("Europe/Amsterdam"),
dr.tz_convert("Europe/Brussels"),
dr.tz_convert("Europe/Oslo"),
dr.tz_convert("Europe/Paris"),
dr.tz_convert("Europe/Rome"),
]

for i, s in enumerate(cs):
t_start, t_stop = sorted(s.iloc[np.random.randint(0, n, 2)].index)
t_start = t_start.tz_convert(cs[(i + 1) % len(cs)].index.tz)
t_stop = t_stop.tz_convert(cs[(i + 1) % len(cs)].index.tz)

hf_data_dict = construct_hf_data_dict(s.index, s.values)
start_idx, end_idx = PlotlyAggregatorParser.get_start_end_indices(
hf_data_dict, hf_data_dict["axis_type"], t_start, t_stop
)



def test_different_tz_no_tz_series_slicing():
n = 60 * 60 * 24 * 3
Expand Down

0 comments on commit f38a5c1

Please sign in to comment.