From 9399ed46b3410a8f2d65204285d86db96057d0b5 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 24 Oct 2020 16:44:17 -0700 Subject: [PATCH 1/6] Adjust tests to use updated pandas syntax for offsets --- xarray/core/groupby.py | 2 +- xarray/tests/test_dataarray.py | 10 +++++++--- xarray/tests/test_dataset.py | 12 +++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 9cdd86e8122..e1e5a0fabe8 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -814,7 +814,7 @@ def apply(self, func, shortcut=False, args=(), **kwargs): ) return self.map(func, shortcut=shortcut, args=args, **kwargs) - def _combine(self, applied, restore_coord_dims=False, shortcut=False): + def _combine(self, applied, shortcut=False): """Recombine the applied objects like the original.""" applied_example, applied = peek_at(applied) coord, dim, positions = self._infer_concat_args(applied_example) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index ea142bb982c..4305cb2c6bf 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -7,6 +7,8 @@ import numpy as np import pandas as pd import pytest +from pandas.testing import assert_index_equal +from pandas.tseries.frequencies import to_offset import xarray as xr from xarray import ( @@ -2920,9 +2922,11 @@ def test_resample(self): actual = array.resample(time="24H").reduce(np.mean) assert_identical(expected, actual) - actual = array.resample(time="24H", loffset="-12H").mean() - expected = DataArray(array.to_series().resample("24H", loffset="-12H").mean()) - assert_identical(expected, actual) + actual = array.resample(time="24H").mean("time").indexes["time"] + to_offset( + "-12H" + ) + expected = array.to_series().resample("24H").mean().index + to_offset("-12H") + assert_index_equal(actual, expected) with raises_regex(ValueError, "index must be monotonic"): array[[2, 0, 1]].resample(time="1D") diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index f1b51766831..d6eb38b0060 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -9,6 +9,8 @@ import pandas as pd import pytest from pandas.core.indexes.datetimes import DatetimeIndex +from pandas.testing import assert_index_equal +from pandas.tseries.frequencies import to_offset import xarray as xr from xarray import ( @@ -3839,11 +3841,11 @@ def test_resample_loffset(self): ) ds.attrs["dsmeta"] = "dsdata" - actual = ds.resample(time="24H", loffset="-12H").mean("time").time - expected = xr.DataArray( - ds.bar.to_series().resample("24H", loffset="-12H").mean() - ).time - assert_identical(expected, actual) + actual = ds.resample(time="24H").mean("time").indexes["time"] + to_offset( + "-12H" + ) + expected = ds.bar.to_series().resample("24H").mean().index + to_offset("-12H") + assert_index_equal(actual, expected) def test_resample_by_mean_discarding_attrs(self): times = pd.date_range("2000-01-01", freq="6H", periods=10) From d25e98398e7f37ce72b5c968a79e0035c9b4a6bf Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 24 Oct 2020 17:05:14 -0700 Subject: [PATCH 2/6] Revert irrelevant change --- xarray/core/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index e1e5a0fabe8..9cdd86e8122 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -814,7 +814,7 @@ def apply(self, func, shortcut=False, args=(), **kwargs): ) return self.map(func, shortcut=shortcut, args=args, **kwargs) - def _combine(self, applied, shortcut=False): + def _combine(self, applied, restore_coord_dims=False, shortcut=False): """Recombine the applied objects like the original.""" applied_example, applied = peek_at(applied) coord, dim, positions = self._infer_concat_args(applied_example) From bbdd764247d89ef6f36e30f4235158f43506caaf Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Wed, 18 Nov 2020 17:32:08 -0800 Subject: [PATCH 3/6] Retain calcs for `actual` --- xarray/tests/test_dataarray.py | 6 +++--- xarray/tests/test_dataset.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 4305cb2c6bf..33f2495659b 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2922,9 +2922,9 @@ def test_resample(self): actual = array.resample(time="24H").reduce(np.mean) assert_identical(expected, actual) - actual = array.resample(time="24H").mean("time").indexes["time"] + to_offset( - "-12H" - ) + # Our use of `loffset` may change if we align our API with pandas' changes. + # ref https://github.com/pydata/xarray/pull/4537 + actual = array.resample(time="24H", loffset="-12H").mean() expected = array.to_series().resample("24H").mean().index + to_offset("-12H") assert_index_equal(actual, expected) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index d6eb38b0060..54a7b5d418d 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3841,9 +3841,9 @@ def test_resample_loffset(self): ) ds.attrs["dsmeta"] = "dsdata" - actual = ds.resample(time="24H").mean("time").indexes["time"] + to_offset( - "-12H" - ) + # Our use of `loffset` may change if we align our API with pandas' changes. + # ref https://github.com/pydata/xarray/pull/4537 + actual = ds.resample(time="24H", loffset="-12H").mean() expected = ds.bar.to_series().resample("24H").mean().index + to_offset("-12H") assert_index_equal(actual, expected) From 376fb74dc5bc20b778a53ab3180cae0a164c2edb Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Mon, 11 Jan 2021 18:04:20 -0800 Subject: [PATCH 4/6] Update xarray/tests/test_dataarray.py Co-authored-by: keewis --- xarray/tests/test_dataarray.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 2f17003f279..a52bf65bc90 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2966,8 +2966,10 @@ def test_resample(self): # Our use of `loffset` may change if we align our API with pandas' changes. # ref https://github.com/pydata/xarray/pull/4537 actual = array.resample(time="24H", loffset="-12H").mean() - expected = array.to_series().resample("24H").mean().index + to_offset("-12H") - assert_index_equal(actual, expected) + expected_ = array.to_series().resample("24H").mean() + expected_.index += to_offset("-12H") + expected = DataArray.from_series(expected_) + assert_identical(actual, expected) with raises_regex(ValueError, "index must be monotonic"): array[[2, 0, 1]].resample(time="1D") From eba911f2dd86509a6ca5caa0f47362a88ea2e7e6 Mon Sep 17 00:00:00 2001 From: keewis Date: Sat, 27 Feb 2021 22:42:01 +0100 Subject: [PATCH 5/6] Apply suggestions from code review --- xarray/tests/test_dataset.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 496f5be4d16..52210443d71 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -9,7 +9,6 @@ import pandas as pd import pytest from pandas.core.indexes.datetimes import DatetimeIndex -from pandas.testing import assert_index_equal from pandas.tseries.frequencies import to_offset import xarray as xr @@ -3830,9 +3829,11 @@ def test_resample_loffset(self): # Our use of `loffset` may change if we align our API with pandas' changes. # ref https://github.com/pydata/xarray/pull/4537 - actual = ds.resample(time="24H", loffset="-12H").mean() - expected = ds.bar.to_series().resample("24H").mean().index + to_offset("-12H") - assert_index_equal(actual, expected) + actual = ds.resample(time="24H", loffset="-12H").mean().bar + expected_ = ds.bar.to_series().resample("24H").mean() + expected_.index += to_offset("-12H") + expected = DataArray.from_series(expected_) + assert_identical(actual, expected) def test_resample_by_mean_discarding_attrs(self): times = pd.date_range("2000-01-01", freq="6H", periods=10) From d920e40164957a78159f47721ee184ea8ae1bb90 Mon Sep 17 00:00:00 2001 From: Keewis Date: Sat, 27 Feb 2021 22:43:04 +0100 Subject: [PATCH 6/6] fix imports --- xarray/tests/test_dataarray.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index a52bf65bc90..64285055058 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -7,7 +7,6 @@ import numpy as np import pandas as pd import pytest -from pandas.testing import assert_index_equal from pandas.tseries.frequencies import to_offset import xarray as xr