Skip to content

Commit

Permalink
cftime tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Nov 15, 2024
1 parent 8773faf commit 879af59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions xarray/groupers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ""
Expand Down
26 changes: 21 additions & 5 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3182,23 +3182,39 @@ 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"),
pytest.param(["JJAS"], id="jjas-only"),
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()

Expand Down Expand Up @@ -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")
)
Expand Down

0 comments on commit 879af59

Please sign in to comment.