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

Extend TimeSeries.resample() to support more resampling methods #2643

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jonasblanc
Copy link

Checklist before merging this PR:

  • Mentioned all issues that this PR fixes or addresses.
  • Summarized the updates of this PR under Summary.
  • Added an entry under Unreleased in the Changelog.

Fixes #699

Summary

  • Expose all aggregation functions of xarray.DataArrayResample through the "method" argument of TimeSeries.resample().
  • Let users specify method specific arguments through method_args.
  • Extend tests with at least one test per additional method.

Other Information

  • Not all methods make sense for either down/up-sampling. I chose not to restrict the use of such methods in situation where they are not expected to be used (ie. xarray is responsible for handling such case). However while testing I encountered this bug: issue xarray asfreq which happens when asfreq is used for down-sampling.
  • I chose to give access to any() and all() aggregation methods, to be consistent with TimeSeries, I cast bool back to int after the aggregation.
  • I chose to let pad as the default method so as not to break the default behavior, even though it's not the most intuitive method for down-sampling.

Copy link
Collaborator

@madtoinou madtoinou left a comment

Choose a reason for hiding this comment

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

Nice contribution @jonasblanc, some minor comments to improve the readibility

'pad': propagate last valid observation forward to next valid

'backfill': use NEXT valid observation to fill.
Method to either aggregate grouped values (for down-sampling) or fill holes (for up-sampling)
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's chaneg the type from str to Literal, it will simplify the sanity checks. Since there are a lot of possible values, we could use a constant/alias to store them.

Copy link
Author

Choose a reason for hiding this comment

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

As discussed waiting for @dennisbader opinion on this. Typing as Literal is not sufficient for sanity check of method. So it would look something like this.

ResamplingMethod = Literal["all", ... "var",]

def resample(
        self, freq: Union[str, pd.DateOffset], method: ResamplingMethod = "pad", **kwargs
    ) -> Self:
    ...
    if method in get_args(ResamplingMethod):
    ...

darts/timeseries.py Outdated Show resolved Hide resolved
darts/timeseries.py Show resolved Hide resolved
Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.17%. Comparing base (0a52490) to head (018c386).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2643      +/-   ##
==========================================
- Coverage   94.20%   94.17%   -0.04%     
==========================================
  Files         141      141              
  Lines       15491    15494       +3     
==========================================
- Hits        14594    14592       -2     
- Misses        897      902       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend TimeSeries.resample() to be more like pandas resample()
2 participants