Skip to content

Resample nearest #17496

Closed
Closed
@shoyer

Description

@shoyer

pandas supports "pad" (forward fill) and "backfill" upsampling, but not "nearest" upsampling. This could be a nice feature to have, and should be pretty easy, too, as the underlying Index.get_indexer method already supports method='nearest'.

Possibly this would be as easy as adding only a few lines to the Resampler class in pandas/core/resample.py. Just monkey-patching this one-liner method seems to work:

In [47]: def nearest(self, limit=None):
    ...:     return self._upsample('nearest', limit=limit)
    ...:

In [48]: pd.core.resample.Resampler.nearest = nearest

In [49]: index = pd.date_range('1/1/2000', periods=9, freq='T')
    ...:

In [50]: series = pd.Series(range(9), index=index)

In [51]: series.resample('20s').nearest()[:5]
Out[51]:
2000-01-01 00:00:00    0
2000-01-01 00:00:20    0
2000-01-01 00:00:40    1
2000-01-01 00:01:00    1
2000-01-01 00:01:20    1
Freq: 20S, dtype: int64

Obviously this needs tests and documentation. Potetially this could be a good project for a new contributor.

This came up in pydata/xarray#1272 where we are copying the new pandas resample() API to xarray.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions