Skip to content

Commit ef0d4e9

Browse files
jonasvddjvdd
andauthored
Datetime range (#213)
* 🙈 remove legacy text * ✨ fix some pandas tests + test for #212 * 🔍 review code --------- Co-authored-by: jvdd <[email protected]>
1 parent 4157229 commit ef0d4e9

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

README.md

-7
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,6 @@ The paper about the plotly-resampler toolkit itself (preprint): https://arxiv.or
179179
code: https://github.com/predict-idlab/MinMaxLTTB
180180

181181

182-
183-
## Future work 🔨
184-
185-
- [x] Support `.add_traces()` (currently only `.add_trace` is supported)
186-
- [x] Support `hf_color` and `hf_markersize`, see [#148](https://github.com/predict-idlab/plotly-resampler/pull/148)
187-
- [x] Integrate with [tsdownsample](https://github.com/predict-idlab/tsdownsample) :racehorse:
188-
189182
<br>
190183

191184
---

plotly_resampler/aggregation/plotly_aggregator_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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.zone == reference_tz.zone
41+
assert ts.tz.__str__() == reference_tz.__str__()
4242
return ts
4343
else: # localize -> time remains the same
4444
return ts.tz_localize(reference_tz)

tests/test_figure_resampler.py

+51-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import multiprocessing
88
import subprocess
99
import time
10+
from datetime import timedelta
1011
from typing import List
1112

1213
import numpy as np
@@ -124,12 +125,16 @@ def test_various_dtypes(float_series):
124125
np.float64,
125126
]
126127
for dtype in valid_dtype_list:
128+
if np.issubdtype(dtype, np.unsignedinteger):
129+
fsv = float_series.astype("int").astype(dtype)
130+
else:
131+
fsv = float_series.astype(dtype)
127132
fig = FigureResampler(go.Figure(), default_n_shown_samples=1000)
128133
# nb. datapoints > default_n_shown_samples
129134
fig.add_trace(
130135
go.Scatter(name="float_series"),
131-
hf_x=float_series.index,
132-
hf_y=float_series.astype(dtype),
136+
hf_x=fsv.index,
137+
hf_y=fsv,
133138
)
134139
fig.full_figure_for_development()
135140

@@ -627,6 +632,50 @@ def test_set_hfx_tz_aware_series():
627632
assert all(fr.hf_data[0]["x"] == pd.DatetimeIndex(df.timestamp))
628633

629634

635+
def test_tz_xaxis_range():
636+
# test related to issue 212 - github.com/predict-idlab/plotly-resampler/issues/212
637+
n = 50_000
638+
s = pd.Series(
639+
index=pd.date_range("2020-01-01", periods=n, freq="1min", tz="UTC"),
640+
data=23 + np.random.randn(n),
641+
)
642+
643+
fig = go.Figure(
644+
layout=go.Layout(
645+
title=dict(
646+
text="AirT test timeseries",
647+
y=0.98,
648+
x=0.5,
649+
xanchor="center",
650+
yanchor="top",
651+
),
652+
xaxis=dict(title="Time", type="date"),
653+
yaxis=dict(title="Air Temp (ºC)", range=[20, 30], fixedrange=True),
654+
template="seaborn",
655+
margin=dict(l=50, r=50, t=50, b=50, pad=5),
656+
showlegend=True,
657+
)
658+
)
659+
660+
fr = FigureResampler(fig, verbose=True, default_n_shown_samples=2000)
661+
fr.add_trace(go.Scattergl(name="AirT", mode="markers"), hf_x=s.index, hf_y=s)
662+
fr.add_trace(go.Scattergl(name="AirT", mode="markers", x=s.index, y=s))
663+
664+
fr.add_vline(x=s.index[0])
665+
fr.add_vline(x=s.index[-1])
666+
667+
start = s.index[0] - timedelta(hours=48)
668+
end = s.index[-1] + timedelta(hours=48)
669+
670+
fr.update_xaxes(range=[start, end])
671+
672+
# verify whether the update was performed correctly
673+
out = fr.construct_update_data({"xaxis.range[0]": start, "xaxis.range[1]": end})
674+
assert len(out) == 3
675+
assert len(out[1]["x"]) == 2000
676+
assert len(out[2]["x"]) == 2000
677+
678+
630679
def test_datetime_hf_x_no_index():
631680
df = pd.DataFrame(
632681
{"timestamp": pd.date_range("2020-01-01", "2020-01-02", freq="1s")}

0 commit comments

Comments
 (0)