Skip to content

Support operations with pandas Offset objects #4535

Closed
@max-sixty

Description

@max-sixty

Is your feature request related to a problem? Please describe.

Currently xarray objects containting datetimes don't operate with pandas' offset objects:

times = pd.date_range("2000-01-01", freq="6H", periods=10)
ds = xr.Dataset(
    {
        "foo": (["time", "x", "y"], np.random.randn(10, 5, 3)),
        "bar": ("time", np.random.randn(10), {"meta": "data"}),
        "time": times,
    }
)
ds.attrs["dsmeta"] = "dsdata"
ds.resample(time="24H").mean("time").time + to_offset("8H")

raises:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-f9de46fe6c54> in <module>
----> 1 ds.resample(time="24H").mean("time").time + to_offset("8H")

/usr/local/lib/python3.8/site-packages/xarray/core/dataarray.py in func(self, other)
   2763 
   2764             variable = (
-> 2765                 f(self.variable, other_variable)
   2766                 if not reflexive
   2767                 else f(other_variable, self.variable)

/usr/local/lib/python3.8/site-packages/xarray/core/variable.py in func(self, other)
   2128             with np.errstate(all="ignore"):
   2129                 new_data = (
-> 2130                     f(self_data, other_data)
   2131                     if not reflexive
   2132                     else f(other_data, self_data)

TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'pandas._libs.tslibs.offsets.Hour'

This is an issue because pandas resampling has deprecated loffset — from our test suite:

xarray/tests/test_dataset.py::TestDataset::test_resample_loffset
  /Users/maximilian/workspace/xarray/xarray/tests/test_dataset.py:3844: FutureWarning: 'loffset' in .resample() and in Grouper() is deprecated.

  >>> df.resample(freq="3s", loffset="8H")

  becomes:

  >>> from pandas.tseries.frequencies import to_offset
  >>> df = df.resample(freq="3s").mean()
  >>> df.index = df.index.to_timestamp() + to_offset("8H")

    ds.bar.to_series().resample("24H", loffset="-12H").mean()

...and so we'll need to support something like this in order to maintain existing behavior.

Describe the solution you'd like
I'm not completely sure; I think probably supporting the operations between xarray objects containing datetime objects and pandas' offset objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions