Skip to content

Commit 99b5845

Browse files
DHRUVA KUMAR KAUSHALDHRUVA KUMAR KAUSHAL
authored andcommitted
test error comment resolves
1 parent 414c5f4 commit 99b5845

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

xarray/tests/test_dataset.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,30 @@ def test_chunks_does_not_load_data(self) -> None:
11411141
[(True, "standard"), (False, "standard"), (True, "noleap"), (True, "360_day")],
11421142
)
11431143
def test_chunk_by_season_resampler(self, use_cftime: bool, calendar: str) -> None:
1144+
# With 2 years of data starting Jan 1, we get 9 seasonal chunks:
1145+
# partial DJF (Jan-Feb), MAM, JJA, SON, DJF, MAM, JJA, SON, partial DJF (Dec)
1146+
ds = xr.Dataset(
1147+
{"foo": (("x", "time"), np.ones((10, 365 * 2)))},
1148+
coords={
1149+
"x": np.arange(10),
1150+
"time": pd.date_range("2000-01-01", periods=365 * 2),
1151+
},
1152+
)
1153+
rechunked = ds.chunk({"x": 2, "time": SeasonResampler()})
1154+
assert len(rechunked.chunksizes["time"]) == 9
1155+
assert rechunked.chunksizes["x"] == (2,) * 5
1156+
# Write out the actual chunks tuple for clarity
1157+
assert rechunked.chunksizes["time"] == (31, 92, 92, 92, 31, 92, 92, 92, 31)
1158+
1159+
# Test custom seasons
1160+
rechunked = ds.chunk(
1161+
{"x": 2, "time": SeasonResampler(["DJFM", "AM", "JJA", "SON"])}
1162+
)
1163+
# Custom seasons also produce boundary chunks
1164+
assert len(rechunked.chunksizes["time"]) == 9
1165+
assert rechunked.chunksizes["x"] == (2,) * 5
1166+
# Write out the actual chunks tuple for clarity
1167+
assert rechunked.chunksizes["time"] == (120, 61, 92, 92, 120, 61, 92, 92, 120)
11441168
"""Test chunking using SeasonResampler."""
11451169
import dask.array
11461170

@@ -1164,13 +1188,6 @@ def test_chunk_by_season_resampler(self, use_cftime: bool, calendar: str) -> Non
11641188
coords={"time": time},
11651189
)
11661190

1167-
# Test standard seasons
1168-
rechunked = ds.chunk(x=2, time=SeasonResampler(["DJF", "MAM", "JJA", "SON"]))
1169-
# With 2 years of data starting Jan 1, we get 9 seasonal chunks:
1170-
# partial DJF (Jan-Feb), MAM, JJA, SON, DJF, MAM, JJA, SON, partial DJF (Dec)
1171-
assert len(rechunked.chunksizes["time"]) == 9
1172-
assert rechunked.chunksizes["x"] == (2,) * 5
1173-
11741191
# Test custom seasons
11751192
rechunked = ds.chunk(
11761193
{"x": 2, "time": SeasonResampler(["DJFM", "AM", "JJA", "SON"])}
@@ -1193,6 +1210,13 @@ def test_chunk_by_season_resampler(self, use_cftime: bool, calendar: str) -> Non
11931210

11941211
@requires_dask
11951212
def test_chunk_by_season_resampler_errors(self):
1213+
# Test error on missing season (should fail with incomplete seasons)
1214+
ds = Dataset(
1215+
{"x": ("time", np.arange(12))},
1216+
coords={"time": pd.date_range("2000-01-01", periods=12, freq="M")},
1217+
)
1218+
with pytest.raises(ValueError, match="do not cover all 12 months"):
1219+
ds.chunk(x=SeasonResampler(["DJF", "MAM", "SON"]))
11961220
"""Test error handling for SeasonResampler chunking."""
11971221
ds = Dataset({"foo": ("x", [1, 2, 3])})
11981222

0 commit comments

Comments
 (0)