Skip to content

Commit 8ee12f6

Browse files
Update resample time offset FutureWarning and docs (#8479)
* Improve FutureWarning re: resample() loffset parameter As discussed in #8175 * Add docs re: resample time offset arithmetic Illustrate 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()` re: issue #7596 and discussion #8175 * Add loffset deprecation warning to resample docstrings * Add docs change to whats-new.rst * Drop redundant FutureWarning in warning text * Change deprecation warning to present tense * Add code example for FutureWarning message --------- Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent dc0931a commit 8ee12f6

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

doc/user-guide/time-series.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ Data that has indices outside of the given ``tolerance`` are set to ``NaN``.
245245
246246
ds.resample(time="1h").nearest(tolerance="1h")
247247
248+
It is often desirable to center the time values after a resampling operation.
249+
That can be accomplished by updating the resampled dataset time coordinate values
250+
using time offset arithmetic via the `pandas.tseries.frequencies.to_offset`_ function.
251+
252+
.. _pandas.tseries.frequencies.to_offset: https://pandas.pydata.org/docs/reference/api/pandas.tseries.frequencies.to_offset.html
253+
254+
.. ipython:: python
255+
256+
resampled_ds = ds.resample(time="6h").mean()
257+
offset = pd.tseries.frequencies.to_offset("6h") / 2
258+
resampled_ds["time"] = resampled_ds.get_index("time") + offset
259+
resampled_ds
248260
249261
For more examples of using grouped operations on a time dimension, see
250262
:doc:`../examples/weather-data`.

doc/whats-new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Bug fixes
5757
Documentation
5858
~~~~~~~~~~~~~
5959

60+
- Added illustration of updating the time coordinate values of a resampled dataset using
61+
time offset arithmetic.
62+
This is the recommended technique to replace the use of the deprecated ``loffset`` parameter
63+
in ``resample`` (:pull:`8479`).
64+
By `Doug Latornell <https://github.com/douglatornell>`_.
65+
6066
- Improved error message when attempting to get a variable which doesn't exist from a Dataset.
6167
(:pull:`8474`)
6268
By `Maximilian Roos <https://github.com/max-sixty>`_.

xarray/core/common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,11 @@ def _resample(
10101010

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

xarray/core/dataarray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7032,6 +7032,12 @@ def resample(
70327032
loffset : timedelta or str, optional
70337033
Offset used to adjust the resampled time labels. Some pandas date
70347034
offset strings are supported.
7035+
7036+
.. deprecated:: 2023.03.0
7037+
Following pandas, the ``loffset`` parameter is deprecated in favor
7038+
of using time offset arithmetic, and will be removed in a future
7039+
version of xarray.
7040+
70357041
restore_coord_dims : bool, optional
70367042
If True, also restore the dimension order of multi-dimensional
70377043
coordinates.

xarray/core/dataset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10382,6 +10382,12 @@ def resample(
1038210382
loffset : timedelta or str, optional
1038310383
Offset used to adjust the resampled time labels. Some pandas date
1038410384
offset strings are supported.
10385+
10386+
.. deprecated:: 2023.03.0
10387+
Following pandas, the ``loffset`` parameter is deprecated in favor
10388+
of using time offset arithmetic, and will be removed in a future
10389+
version of xarray.
10390+
1038510391
restore_coord_dims : bool, optional
1038610392
If True, also restore the dimension order of multi-dimensional
1038710393
coordinates.

0 commit comments

Comments
 (0)