From 879af5950049ef791388e2ec7d672e7dbe107387 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 14 Nov 2024 20:58:56 -0700 Subject: [PATCH] cftime tests --- xarray/groupers.py | 4 ++-- xarray/tests/test_groupby.py | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/xarray/groupers.py b/xarray/groupers.py index 3c657884634..3a1ea1aebea 100644 --- a/xarray/groupers.py +++ b/xarray/groupers.py @@ -876,8 +876,8 @@ def factorize(self, group: T_Group) -> EncodedGroups: # all years and seasons def get_label(year, season): - month = season_tuples[season][0] - return f"{year}-{month}-01" + month, *_ = season_tuples[season] + return f"{year}-{month:02d}-01" unique_codes = np.arange(len(unique_coord)) valid_season_mask = season_label != "" diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index a8fb905100a..ca27b3643d6 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -3182,13 +3182,21 @@ def test_season_resampling_raises_unsorted_seasons(self, seasons): with pytest.raises(ValueError, match="sort"): da.resample(time=SeasonResampler(seasons)) - # TODO: drop_incomplete - @requires_cftime + @pytest.mark.parametrize( + "use_cftime", + [ + pytest.param( + True, marks=pytest.mark.skipif(not has_cftime, reason="no cftime") + ), + False, + ], + ) @pytest.mark.parametrize("drop_incomplete", [True, False]) @pytest.mark.parametrize( "seasons", [ pytest.param(["DJF", "MAM", "JJA", "SON"], id="standard"), + pytest.param(["NDJ", "FMA", "MJJ", "ASO"], id="nov-first"), pytest.param(["MAM", "JJA", "SON", "DJF"], id="standard-diff-order"), pytest.param(["JFM", "AMJ", "JAS", "OND"], id="december-same-year"), pytest.param(["DJF", "MAM", "JJA", "ON"], id="skip-september"), @@ -3196,9 +3204,17 @@ def test_season_resampling_raises_unsorted_seasons(self, seasons): pytest.param(["MAM", "JJA", "SON", "DJF"], id="different-order"), ], ) - def test_season_resampler(self, seasons: list[str], drop_incomplete: bool) -> None: + def test_season_resampler( + self, seasons: list[str], drop_incomplete: bool, use_cftime: bool + ) -> None: calendar = "standard" - time = date_range("2001-01-01", "2002-12-30", freq="D", calendar=calendar) + time = date_range( + "2001-01-01", + "2002-12-30", + freq="D", + calendar=calendar, + use_cftime=use_cftime, + ) da = DataArray(np.ones(time.size), dims="time", coords={"time": time}) counts = da.resample(time="ME").count() @@ -3239,7 +3255,7 @@ def test_season_resampler(self, seasons: list[str], drop_incomplete: bool) -> No # we construct expected in the standard calendar xr.DataArray(expected_vals, dims="time", coords={"time": expected_time}) # and then convert to the expected calendar, - .convert_calendar(calendar, align_on="date") + .convert_calendar(calendar, align_on="date", use_cftime=use_cftime) # and finally sort since DJF will be out-of-order .sortby("time") )