| 2352 | ], |
| 2353 | ) |
| 2354 | def test_resample( |
| 2355 | self, use_cftime: bool, resample_freq: ResampleCompatible |
| 2356 | ) -> None: |
| 2357 | if use_cftime and not has_cftime: |
| 2358 | pytest.skip() |
| 2359 | times = xr.date_range( |
| 2360 | "2000-01-01", freq="6h", periods=10, use_cftime=use_cftime |
| 2361 | ) |
| 2362 | |
| 2363 | def resample_as_pandas(ds, *args, **kwargs): |
| 2364 | ds_ = ds.copy(deep=True) |
| 2365 | if use_cftime: |
| 2366 | ds_["time"] = times.to_datetimeindex(time_unit="ns") |
| 2367 | result = Dataset.from_dataframe( |
| 2368 | ds_.to_dataframe().resample(*args, **kwargs).mean() |
| 2369 | ) |
| 2370 | if use_cftime: |
| 2371 | result = result.convert_calendar( |
| 2372 | calendar="standard", use_cftime=use_cftime |
| 2373 | ) |
| 2374 | return result |
| 2375 | |
| 2376 | ds = Dataset( |
| 2377 | { |
| 2378 | "foo": ("time", np.random.randint(1, 1000, 10)), |
| 2379 | "bar": ("time", np.random.randint(1, 1000, 10)), |
| 2380 | "time": times, |
| 2381 | } |
| 2382 | ) |
| 2383 | |
| 2384 | actual = ds.resample(time=resample_freq).mean() |
| 2385 | expected = resample_as_pandas(ds, resample_freq) |
| 2386 | assert_identical(expected, actual) |
| 2387 | |
| 2388 | actual = ds.resample(time=resample_freq).reduce(np.mean) |
| 2389 | assert_identical(expected, actual) |
| 2390 | |
| 2391 | actual = ds.resample(time=resample_freq, closed="right").mean() |
| 2392 | expected = resample_as_pandas(ds, resample_freq, closed="right") |
| 2393 | assert_identical(expected, actual) |
| 2394 | |
| 2395 | with pytest.raises(ValueError, match=r"Index must be monotonic"): |
| 2396 | ds.isel(time=[2, 0, 1]).resample(time=resample_freq) |
| 2397 | |
| 2398 | reverse = ds.isel(time=slice(-1, None, -1)) |
| 2399 | with pytest.raises(ValueError): |
| 2400 | reverse.resample(time=resample_freq).mean() |
| 2401 | |
| 2402 | def test_resample_and_first(self) -> None: |
| 2403 | times = pd.date_range("2000-01-01", freq="6h", periods=10) |