Skip to content

Datetime range #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ The paper about the plotly-resampler toolkit itself (preprint): https://arxiv.or
code: https://github.com/predict-idlab/MinMaxLTTB



## Future work 🔨

- [x] Support `.add_traces()` (currently only `.add_trace` is supported)
- [x] Support `hf_color` and `hf_markersize`, see [#148](https://github.com/predict-idlab/plotly-resampler/pull/148)
- [x] Integrate with [tsdownsample](https://github.com/predict-idlab/tsdownsample) :racehorse:

<br>

---
Expand Down
2 changes: 1 addition & 1 deletion plotly_resampler/aggregation/plotly_aggregator_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def to_same_tz(
return None
elif reference_tz is not None:
if ts.tz is not None:
assert ts.tz.zone == reference_tz.zone
assert ts.tz.__str__() == reference_tz.__str__()
return ts
else: # localize -> time remains the same
return ts.tz_localize(reference_tz)
Expand Down
53 changes: 51 additions & 2 deletions tests/test_figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import multiprocessing
import subprocess
import time
from datetime import timedelta
from typing import List

import numpy as np
Expand Down Expand Up @@ -124,12 +125,16 @@ def test_various_dtypes(float_series):
np.float64,
]
for dtype in valid_dtype_list:
if np.issubdtype(dtype, np.unsignedinteger):
fsv = float_series.astype("int").astype(dtype)
else:
fsv = float_series.astype(dtype)
fig = FigureResampler(go.Figure(), default_n_shown_samples=1000)
# nb. datapoints > default_n_shown_samples
fig.add_trace(
go.Scatter(name="float_series"),
hf_x=float_series.index,
hf_y=float_series.astype(dtype),
hf_x=fsv.index,
hf_y=fsv,
)
fig.full_figure_for_development()

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


def test_tz_xaxis_range():
# test related to issue 212 - github.com/predict-idlab/plotly-resampler/issues/212
n = 50_000
s = pd.Series(
index=pd.date_range("2020-01-01", periods=n, freq="1min", tz="UTC"),
data=23 + np.random.randn(n),
)

fig = go.Figure(
layout=go.Layout(
title=dict(
text="AirT test timeseries",
y=0.98,
x=0.5,
xanchor="center",
yanchor="top",
),
xaxis=dict(title="Time", type="date"),
yaxis=dict(title="Air Temp (ºC)", range=[20, 30], fixedrange=True),
template="seaborn",
margin=dict(l=50, r=50, t=50, b=50, pad=5),
showlegend=True,
)
)

fr = FigureResampler(fig, verbose=True, default_n_shown_samples=2000)
fr.add_trace(go.Scattergl(name="AirT", mode="markers"), hf_x=s.index, hf_y=s)
fr.add_trace(go.Scattergl(name="AirT", mode="markers", x=s.index, y=s))

fr.add_vline(x=s.index[0])
fr.add_vline(x=s.index[-1])

start = s.index[0] - timedelta(hours=48)
end = s.index[-1] + timedelta(hours=48)

fr.update_xaxes(range=[start, end])

# verify whether the update was performed correctly
out = fr.construct_update_data({"xaxis.range[0]": start, "xaxis.range[1]": end})
assert len(out) == 3
assert len(out[1]["x"]) == 2000
assert len(out[2]["x"]) == 2000


def test_datetime_hf_x_no_index():
df = pd.DataFrame(
{"timestamp": pd.date_range("2020-01-01", "2020-01-02", freq="1s")}
Expand Down