predict-idlab / plotly-resampler

Visualize large time series data with plotly.py
https://predict-idlab.github.io/plotly-resampler/latest
MIT License
1k stars 67 forks source link

Resample Older Data #222

Closed firmai closed 10 months ago

firmai commented 1 year ago

Thanks for the software, I want to resample older data, say 20 years, but want to keep latest data say 3 months in its daily format. This would be a great addition if it doesn't exist yet? Gives the benefit of both worlds.

Let me know what I can do to implement this, I can work on it over the weekend.

Best, Derek

jonasvdd commented 1 year ago

Hi @firmai

I think, just splitting up the data, based on the 3 months threshold, and adding the to-be-aggregated and to-be-retained data separately, should do the trick.

See code example ⬇️ :

import pandas as pd
import plotly.graph_objs as go
import numpy as np
from plotly_resampler import FigureResampler

# Generate time series data
count = 500_000
arange = np.arange(count) / (count / 100)
s = pd.Series(
    index=pd.date_range(start="2014-01-01", periods=count, freq="5 min"),
    data=(np.sin(arange) + 2) * arange + np.random.randn(count) / 10 * arange,
)

# define until where you would like to aggregate the data
# data before this threshold will be aggregated, data after will be shown as-is
resample_index_th = s.index[-1] - pd.Timedelta(days=90)

# Create the figure
fig = FigureResampler(go.Figure())

# Add the data that will be resampled (i.e., the data b4 resample_index_th)
fig.add_trace(
    go.Scattergl(showlegend=False, line_color="blue", opacity=0.3),
    hf_x=s[:resample_index_th].index,
    hf_y=s[:resample_index_th],
)

# Add the data that will be shown as-is (i.e., the data after resample_index_th)
# note, to make it appear more consistent, we use the same line color and do 
# not show the legend names of the traces
fig.add_trace(
    go.Scattergl(showlegend=False, line_color="blue"),
    hf_x=s[resample_index_th:].index,
    hf_y=s[resample_index_th:],
    # by setting limit to view and max_n_samples, we will not resample this data
    limit_to_view=True,
    max_n_samples=s[resample_index_th:].shape[0] + 1,
)

fig.show_dash(mode="inline")

Please let me know if this meets your requirements, Kind regards, Jonas

jonasvdd commented 10 months ago

I will close this issue for now (as we got no response from @firmai).