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

Update resample time offset FutureWarning and docs #8479

Merged
12 changes: 12 additions & 0 deletions doc/user-guide/time-series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,18 @@ Data that has indices outside of the given ``tolerance`` are set to ``NaN``.

ds.resample(time="1h").nearest(tolerance="1h")

It is often desirable to center the time values after a resampling operation.
That can be accomplished by updating the resampled dataset time coordinate values
using time offset arithmetic via the `pandas.tseries.frequencies.to_offset`_ function.

.. _pandas.tseries.frequencies.to_offset: https://pandas.pydata.org/docs/reference/api/pandas.tseries.frequencies.to_offset.html

.. ipython:: python

resampled_ds = ds.resample(time="6h").mean()
offset = pd.tseries.frequencies.to_offset("6h") / 2
resampled_ds["time"] = resampled_ds.get_index("time") + offset
douglatornell marked this conversation as resolved.
Show resolved Hide resolved
resampled_ds

For more examples of using grouped operations on a time dimension, see
:doc:`../examples/weather-data`.
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Bug fixes
Documentation
~~~~~~~~~~~~~

- Added illustration of updating the time coordinate values of a resampled dataset using
time offset arithmetic.
This is the recommended technique to replace the use of the deprecated ``loffset`` parameter
in ``resample`` (:pull:`8479`).
By `Doug Latornell <https://github.com/douglatornell>`_.

- Improved error message when attempting to get a variable which doesn't exist from a Dataset.
(:pull:`8474`)
By `Maximilian Roos <https://github.com/max-sixty>`_.
Expand Down
7 changes: 5 additions & 2 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,11 @@ def _resample(

if loffset is not None:
emit_user_level_warning(
"Following pandas, the `loffset` parameter to resample will be deprecated "
"in a future version of xarray. Switch to using time offset arithmetic.",
"Following pandas, the `loffset` parameter to resample is deprecated. "
"Switch to updating the resampled dataset time coordinate using "
"time offset arithmetic. For example:\n"
" >>> offset = pd.tseries.frequencies.to_offset(freq) / 2\n"
' >>> resampled_ds["time"] = resampled_ds.get_index("time") + offset',
FutureWarning,
)

Expand Down
6 changes: 6 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7032,6 +7032,12 @@ def resample(
loffset : timedelta or str, optional
Offset used to adjust the resampled time labels. Some pandas date
offset strings are supported.

.. deprecated:: 2023.03.0
Following pandas, the ``loffset`` parameter is deprecated in favor
of using time offset arithmetic, and will be removed in a future
version of xarray.

restore_coord_dims : bool, optional
If True, also restore the dimension order of multi-dimensional
coordinates.
Expand Down
6 changes: 6 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10382,6 +10382,12 @@ def resample(
loffset : timedelta or str, optional
Offset used to adjust the resampled time labels. Some pandas date
offset strings are supported.

.. deprecated:: 2023.03.0
Following pandas, the ``loffset`` parameter is deprecated in favor
of using time offset arithmetic, and will be removed in a future
version of xarray.

restore_coord_dims : bool, optional
If True, also restore the dimension order of multi-dimensional
coordinates.
Expand Down
Loading