|
6 | 6 | import numpy as np
|
7 | 7 | import pandas as pd
|
8 | 8 | import pytest
|
9 |
| -from pandas.tseries.frequencies import to_offset |
10 | 9 |
|
11 | 10 | import xarray as xr
|
12 | 11 | from xarray import DataArray, Dataset, Variable
|
@@ -1860,7 +1859,9 @@ def test_resample_base(self) -> None:
|
1860 | 1859 |
|
1861 | 1860 | with pytest.warns(FutureWarning, match="the `base` parameter to resample"):
|
1862 | 1861 | actual = array.resample(time="24H", base=base).mean()
|
1863 |
| - expected = DataArray(array.to_series().resample("24H", base=base).mean()) |
| 1862 | + expected = DataArray( |
| 1863 | + array.to_series().resample("24H", offset=f"{base}H").mean() |
| 1864 | + ) |
1864 | 1865 | assert_identical(expected, actual)
|
1865 | 1866 |
|
1866 | 1867 | def test_resample_offset(self) -> None:
|
@@ -1897,15 +1898,22 @@ def test_resample_loffset(self, loffset) -> None:
|
1897 | 1898 |
|
1898 | 1899 | with pytest.warns(FutureWarning, match="`loffset` parameter"):
|
1899 | 1900 | actual = array.resample(time="24H", loffset=loffset).mean()
|
1900 |
| - expected = DataArray(array.to_series().resample("24H", loffset=loffset).mean()) |
| 1901 | + series = array.to_series().resample("24H").mean() |
| 1902 | + if not isinstance(loffset, pd.DateOffset): |
| 1903 | + loffset = pd.Timedelta(loffset) |
| 1904 | + series.index = series.index + loffset |
| 1905 | + expected = DataArray(series) |
1901 | 1906 | assert_identical(actual, expected)
|
1902 | 1907 |
|
1903 | 1908 | def test_resample_invalid_loffset(self) -> None:
|
1904 | 1909 | times = pd.date_range("2000-01-01", freq="6H", periods=10)
|
1905 | 1910 | array = DataArray(np.arange(10), [("time", times)])
|
1906 | 1911 |
|
1907 |
| - with pytest.raises(ValueError, match="`loffset` must be"): |
1908 |
| - array.resample(time="24H", loffset=1).mean() # type: ignore |
| 1912 | + with pytest.warns( |
| 1913 | + FutureWarning, match="Following pandas, the `loffset` parameter" |
| 1914 | + ): |
| 1915 | + with pytest.raises(ValueError, match="`loffset` must be"): |
| 1916 | + array.resample(time="24H", loffset=1).mean() # type: ignore |
1909 | 1917 |
|
1910 | 1918 |
|
1911 | 1919 | class TestDatasetResample:
|
@@ -1993,14 +2001,6 @@ def test_resample_loffset(self):
|
1993 | 2001 | )
|
1994 | 2002 | ds.attrs["dsmeta"] = "dsdata"
|
1995 | 2003 |
|
1996 |
| - # Our use of `loffset` may change if we align our API with pandas' changes. |
1997 |
| - # ref https://github.com/pydata/xarray/pull/4537 |
1998 |
| - actual = ds.resample(time="24H", loffset="-12H").mean().bar |
1999 |
| - expected_ = ds.bar.to_series().resample("24H").mean() |
2000 |
| - expected_.index += to_offset("-12H") |
2001 |
| - expected = DataArray.from_series(expected_) |
2002 |
| - assert_allclose(actual, expected) |
2003 |
| - |
2004 | 2004 | def test_resample_by_mean_discarding_attrs(self):
|
2005 | 2005 | times = pd.date_range("2000-01-01", freq="6H", periods=10)
|
2006 | 2006 | ds = Dataset(
|
|
0 commit comments