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

ENH: resample methods with tolerance #2716

Merged
merged 5 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 12 additions & 3 deletions doc/time-series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,20 @@ resampling group:

ds.resample(time='6H').reduce(np.mean)

For upsampling, xarray provides four methods: ``asfreq``, ``ffill``, ``bfill``,
and ``interpolate``. ``interpolate`` extends ``scipy.interpolate.interp1d`` and
supports all of its schemes. All of these resampling operations work on both
For upsampling, xarray provides six methods: ``asfreq``, ``ffill``, ``bfill``, ``pad``,
``nearest`` and ``interpolate``. ``interpolate`` extends ``scipy.interpolate.interp1d``
and supports all of its schemes. All of these resampling operations work on both
Dataset and DataArray objects with an arbitrary number of dimensions.

In order to limit the scope of the methods ``ffill``, ``bfill``, ``pad`` and
Copy link
Contributor

Choose a reason for hiding this comment

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

Mention that "tolerance" is in coordinate (or label) units, not index units and that the index version is limit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The unit for tolerance is now (a878ebc) explicitly mentioned in the time-series example. However, I did not comment on the index method as this has not been implemented for the resample method, yet. The index method seems to be a bigger/other project.

``nearest`` the ``tolerance`` argument can be set. Data that has indices
outside of the given ``tolerance`` are set to ``NaN``.

.. ipython:: python

ds.resample(time='1H').nearest(tolerance='1H')


For more examples of using grouped operations on a time dimension, see
:ref:`toy weather data`.

Expand Down
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Enhancements
report showing what exactly differs between the two objects (dimensions /
coordinates / variables / attributes) (:issue:`1507`).
By `Benoit Bovy <https://github.com/benbovy>`_.
- Add ``tolerance`` option to ``resample()`` methods ``bfill``, ``pad``,
``nearest``. (:issue:`2695`)
By `Hauke Schulz <https://github.com/observingClouds>`_.

Bug fixes
~~~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,13 @@ def resample(self, indexer=None, skipna=None, closed=None, label=None,
array([ 0. , 0.032258, 0.064516, ..., 10.935484, 10.967742, 11. ])
Coordinates:
* time (time) datetime64[ns] 1999-12-15 1999-12-16 1999-12-17 ...

Limit scope of upsampling method
>>> da.resample(time='1D').nearest(tolerance='1D')
<xarray.DataArray (time: 337)>
array([ 0., 0., nan, ..., nan, 11., 11.])
Coordinates:
* time (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-11-15

References
----------
Expand Down
42 changes: 36 additions & 6 deletions xarray/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,53 @@ def asfreq(self):
"""
return self._upsample('asfreq')

def pad(self):
def pad(self, tolerance=None):
"""Forward fill new values at up-sampled frequency.

Parameters
----------
tolerance : optional
Maximum distance between original and new labels to limit
the up-sampling method.
Up-sampled data with indices that satisfy the equation
``abs(index[indexer] - target) <= tolerance`` are filled by
new values. Data with indices that are outside the given
tolerance are filled with ``NaN`` s
"""
return self._upsample('pad')
return self._upsample('pad', tolerance=tolerance)
ffill = pad

def backfill(self):
def backfill(self, tolerance=None):
"""Backward fill new values at up-sampled frequency.

Parameters
----------
tolerance : optional
Maximum distance between original and new labels to limit
the up-sampling method.
Up-sampled data with indices that satisfy the equation
``abs(index[indexer] - target) <= tolerance`` are filled by
new values. Data with indices that are outside the given
tolerance are filled with ``NaN`` s
"""
return self._upsample('backfill')
return self._upsample('backfill', tolerance=tolerance)
bfill = backfill

def nearest(self):
def nearest(self, tolerance=None):
"""Take new values from nearest original coordinate to up-sampled
frequency coordinates.

Parameters
----------
tolerance : optional
Maximum distance between original and new labels to limit
the up-sampling method.
Up-sampled data with indices that satisfy the equation
``abs(index[indexer] - target) <= tolerance`` are filled by
new values. Data with indices that are outside the given
tolerance are filled with ``NaN`` s
"""
return self._upsample('nearest')
return self._upsample('nearest', tolerance=tolerance)

def interpolate(self, kind='linear'):
"""Interpolate up-sampled data using the original data
Expand Down
30 changes: 30 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,36 @@ def test_upsample_nd(self):
('x', 'y', 'time'))
assert_identical(expected, actual)

def test_upsample_tolerance(self):
# Test tolerance keyword for upsample methods bfill, pad, nearest
times = pd.date_range('2000-01-01', freq='1D', periods=2)
times_upsampled = pd.date_range('2000-01-01', freq='6H', periods=5)
array = DataArray(np.arange(2), [('time', times)])

# Forward fill
actual = array.resample(time='6H').ffill(tolerance='12H')
expected = DataArray([0., 0., 0., np.nan, 1.],
[('time', times_upsampled)])
assert_identical(expected, actual)

# Backward fill
actual = array.resample(time='6H').bfill(tolerance='12H')
expected = DataArray([0., np.nan, 1., 1., 1.],
[('time', times_upsampled)])
assert_identical(expected, actual)

# Pad
actual = array.resample(time='6H').pad(tolerance='12H')
expected = DataArray([0., 0., 0., np.nan, 1.],
[('time', times_upsampled)])
assert_identical(expected, actual)
observingClouds marked this conversation as resolved.
Show resolved Hide resolved

# Nearest
actual = array.resample(time='6H').nearest(tolerance='12H')
observingClouds marked this conversation as resolved.
Show resolved Hide resolved
expected = DataArray([0, 0, 1, 1, 1],
[('time', times_upsampled)])
assert_identical(expected, actual)

@requires_scipy
def test_upsample_interpolate(self):
from scipy.interpolate import interp1d
Expand Down