|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import datetime
|
| 4 | +import operator |
4 | 5 | import warnings
|
5 | 6 |
|
6 | 7 | import numpy as np
|
|
17 | 18 | assert_identical,
|
18 | 19 | create_test_data,
|
19 | 20 | has_cftime,
|
| 21 | + has_flox, |
20 | 22 | has_pandas_version_two,
|
21 | 23 | requires_dask,
|
22 | 24 | requires_flox,
|
@@ -2336,3 +2338,37 @@ def test_groupby_binary_op_regression() -> None:
|
2336 | 2338 | anom_gb = x_slice.groupby("time.month") - clim
|
2337 | 2339 |
|
2338 | 2340 | assert_identical(xr.zeros_like(anom_gb), anom_gb)
|
| 2341 | + |
| 2342 | + |
| 2343 | +@requires_flox |
| 2344 | +@pytest.mark.parametrize("func", ["sum", "prod"]) |
| 2345 | +@pytest.mark.parametrize("skipna", [True, False]) |
| 2346 | +@pytest.mark.parametrize("min_count", [None, 1]) |
| 2347 | +def test_min_count_vs_flox(func: str, min_count: int | None, skipna: bool) -> None: |
| 2348 | + da = DataArray( |
| 2349 | + data=np.array([np.nan, 1, 1, np.nan, 1, 1]), |
| 2350 | + dims="x", |
| 2351 | + coords={"labels": ("x", np.array([1, 2, 3, 1, 2, 3]))}, |
| 2352 | + ) |
| 2353 | + |
| 2354 | + gb = da.groupby("labels") |
| 2355 | + method = operator.methodcaller(func, min_count=min_count, skipna=skipna) |
| 2356 | + with xr.set_options(use_flox=True): |
| 2357 | + actual = method(gb) |
| 2358 | + with xr.set_options(use_flox=False): |
| 2359 | + expected = method(gb) |
| 2360 | + assert_identical(actual, expected) |
| 2361 | + |
| 2362 | + |
| 2363 | +@pytest.mark.parametrize("use_flox", [True, False]) |
| 2364 | +def test_min_count_error(use_flox: bool) -> None: |
| 2365 | + if use_flox and not has_flox: |
| 2366 | + pytest.skip() |
| 2367 | + da = DataArray( |
| 2368 | + data=np.array([np.nan, 1, 1, np.nan, 1, 1]), |
| 2369 | + dims="x", |
| 2370 | + coords={"labels": ("x", np.array([1, 2, 3, 1, 2, 3]))}, |
| 2371 | + ) |
| 2372 | + with xr.set_options(use_flox=use_flox): |
| 2373 | + with pytest.raises(TypeError): |
| 2374 | + da.groupby("labels").mean(min_count=1) |
0 commit comments