(dim_num, dtype, dask, func, skipna, aggdim)
| 595 | @pytest.mark.parametrize("skipna", [False, True]) |
| 596 | @pytest.mark.parametrize("aggdim", [None, "x"]) |
| 597 | def test_reduce(dim_num, dtype, dask, func, skipna, aggdim): |
| 598 | if aggdim == "y" and dim_num < 2: |
| 599 | pytest.skip("dim not in this test") |
| 600 | |
| 601 | if dtype == np.bool_ and func == "mean": |
| 602 | pytest.skip("numpy does not support this") |
| 603 | |
| 604 | if dask and not has_dask: |
| 605 | pytest.skip("requires dask") |
| 606 | |
| 607 | if dask and skipna is False and dtype == np.bool_: |
| 608 | pytest.skip("dask does not compute object-typed array") |
| 609 | |
| 610 | rtol = 1e-04 if dtype == np.float32 else 1e-05 |
| 611 | |
| 612 | da = construct_dataarray(dim_num, dtype, contains_nan=True, dask=dask) |
| 613 | axis = None if aggdim is None else da.get_axis_num(aggdim) |
| 614 | |
| 615 | # TODO: remove these after resolving |
| 616 | # https://github.com/dask/dask/issues/3245 |
| 617 | with warnings.catch_warnings(): |
| 618 | warnings.filterwarnings("ignore", "Mean of empty slice") |
| 619 | warnings.filterwarnings("ignore", "All-NaN slice") |
| 620 | warnings.filterwarnings("ignore", "invalid value encountered in") |
| 621 | |
| 622 | if da.dtype.kind == "O" and skipna: |
| 623 | # Numpy < 1.13 does not handle object-type array. |
| 624 | try: |
| 625 | if skipna: |
| 626 | expected = getattr(np, f"nan{func}")(da.values, axis=axis) |
| 627 | else: |
| 628 | expected = getattr(np, func)(da.values, axis=axis) |
| 629 | |
| 630 | actual = getattr(da, func)(skipna=skipna, dim=aggdim) |
| 631 | assert_dask_array(actual, dask) |
| 632 | np.testing.assert_allclose( |
| 633 | actual.values, np.array(expected), rtol=1.0e-4, equal_nan=True |
| 634 | ) |
| 635 | except (TypeError, AttributeError, ZeroDivisionError): |
| 636 | # TODO currently, numpy does not support some methods such as |
| 637 | # nanmean for object dtype |
| 638 | pass |
| 639 | |
| 640 | actual = getattr(da, func)(skipna=skipna, dim=aggdim) |
| 641 | |
| 642 | # for dask case, make sure the result is the same for numpy backend |
| 643 | expected = getattr(da.compute(), func)(skipna=skipna, dim=aggdim) |
| 644 | assert_allclose(actual, expected, rtol=rtol) |
| 645 | |
| 646 | # make sure the compatibility with pandas' results. |
| 647 | if func in ["var", "std"]: |
| 648 | expected = series_reduce(da, func, skipna=skipna, dim=aggdim, ddof=0) |
| 649 | assert_allclose(actual, expected, rtol=rtol) |
| 650 | # also check ddof!=0 case |
| 651 | actual = getattr(da, func)(skipna=skipna, dim=aggdim, ddof=5) |
| 652 | if dask: |
| 653 | assert isinstance(da.data, dask_array_type) |
| 654 | expected = series_reduce(da, func, skipna=skipna, dim=aggdim, ddof=5) |
nothing calls this directly
no test coverage detected
searching dependent graphs…