Skip to content
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

Fix/nan in resample #1350

Merged
merged 8 commits into from
Nov 15, 2022
11 changes: 11 additions & 0 deletions darts/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,17 @@ def test_resample_timeseries(self):
resampled_timeseries.pd_series().at[pd.Timestamp("20130109")], 8
)

# using loffset to avoid nan in the first value
times = pd.date_range(
start=pd.Timestamp("20200101233000"), periods=10, freq="15T"
)
pd_series = pd.Series(range(10), index=times)
timeseries = TimeSeries.from_series(pd_series)
resampled_timeseries = timeseries.resample(freq="1h", loffset="30T")
self.assertEqual(
resampled_timeseries.pd_series().at[pd.Timestamp("20200101233000")], 0
)

def test_short_series_creation(self):
# test missing freq argument error when filling missing dates on short time series
with self.assertRaises(ValueError):
Expand Down
39 changes: 37 additions & 2 deletions darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,8 @@ def add_holidays(
tg.holidays_timeseries(self.time_index, country_code, prov, state)
)

def resample(self, freq: str, method: str = "pad") -> "TimeSeries":
def resample(self, freq: str, method: str = "pad", **kwargs) -> "TimeSeries":

"""
Build a reindexed ``TimeSeries`` with a given frequency.
Provided method is used to fill holes in reindexed TimeSeries, by default 'pad'.
Expand All @@ -2902,13 +2903,47 @@ def resample(self, freq: str, method: str = "pad") -> "TimeSeries":
'pad': propagate last valid observation forward to next valid

'backfill': use NEXT valid observation to fill.
kwargs
some keyword arguments for the `xarray.resample` method, notably `loffset` or `base` to indicate where
to start the resampling and avoid nan at the first value of the resampled TimeSeries
For more informations, see the `xarray resample() documentation
<https://docs.xarray.dev/en/stable/generated/xarray.DataArray.resample.html>`_.

Examples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this, thanks :)

--------
>>> times = pd.date_range(start=pd.Timestamp("20200101233000"), periods=6, freq="15T")
>>> pd_series = pd.Series(range(6), index=times)
>>> ts = TimeSeries.from_series(pd_series)
>>> print(ts.time_index)
DatetimeIndex(['2020-01-01 23:30:00', '2020-01-01 23:45:00',
'2020-01-02 00:00:00', '2020-01-02 00:15:00',
'2020-01-02 00:30:00', '2020-01-02 00:45:00'],
dtype='datetime64[ns]', name='time', freq='15T')
>>> resampled_nokwargs_ts = ts.resample(freq="1h")
>>> print(resampled_nokwargs_ts.time_index)
DatetimeIndex(['2020-01-01 23:00:00', '2020-01-02 00:00:00'],
dtype='datetime64[ns]', name='time', freq='H')
>>> print(resampled_nokwargs_ts.values())
[[nan]
[ 2.]]
>>> resampled_ts = ts.resample(freq="1h", loffset="30T")
>>> print(resampled_ts.time_index)
DatetimeIndex(['2020-01-01 23:30:00', '2020-01-02 00:30:00'],
dtype='datetime64[ns]', name='time', freq='H')
>>> print(resampled_ts.values())
[[0.]
[4.]]

Returns
-------
TimeSeries
A reindexed TimeSeries with given frequency.
"""

resample = self._xa.resample({self._time_dim: freq})
resample = self._xa.resample(
indexer={self._time_dim: freq},
**kwargs,
)

# TODO: check
if method == "pad":
Expand Down