Skip to content

Commit

Permalink
fix: min_counts for non-nan sum and prod (#4423)
Browse files Browse the repository at this point in the history
* fix: min_counts for non-nan sum and prod

* use method1

* typo

* remove empty line

* Update doc/whats-new.rst

Co-authored-by: keewis <keewis@users.noreply.github.com>

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: keewis <keewis@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 2, 2020
1 parent 5f8ddfe commit 333e8db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ Bug fixes
- Fix silently overwriting the `engine` key when passing :py:func:`open_dataset` a file object
to an incompatible netCDF (:issue:`4457`). Now incompatible combinations of files and engines raise
an exception instead. By `Alessandro Amici <https://github.com/alexamici>`_.

- The ``min_count`` argument to :py:meth:`DataArray.sum()` and :py:meth:`DataArray.prod()`
is now ignored when not applicable, i.e. when ``skipna=False`` or when ``skipna=None``
and the dtype does not have a missing value (:issue:`4352`).
By `Mathias Hauser <https://github.com/mathause>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ def f(values, axis=None, skipna=None, **kwargs):
nanname = "nan" + name
func = getattr(nanops, nanname)
else:
if name in ["sum", "prod"]:
kwargs.pop("min_count", None)
func = _dask_or_eager_func(name, dask_module=dask_module)

try:
Expand Down Expand Up @@ -361,7 +363,7 @@ def f(values, axis=None, skipna=None, **kwargs):
median.numeric_only = True
prod = _create_nan_agg_method("prod")
prod.numeric_only = True
sum.available_min_count = True
prod.available_min_count = True
cumprod_1d = _create_nan_agg_method("cumprod")
cumprod_1d.numeric_only = True
cumsum_1d = _create_nan_agg_method("cumsum")
Expand Down
10 changes: 6 additions & 4 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,17 @@ def test_dask_gradient(axis, edge_order):
@pytest.mark.parametrize("dask", [False, True])
@pytest.mark.parametrize("func", ["sum", "prod"])
@pytest.mark.parametrize("aggdim", [None, "x"])
def test_min_count(dim_num, dtype, dask, func, aggdim):
@pytest.mark.parametrize("contains_nan", [True, False])
@pytest.mark.parametrize("skipna", [True, False, None])
def test_min_count(dim_num, dtype, dask, func, aggdim, contains_nan, skipna):
if dask and not has_dask:
pytest.skip("requires dask")

da = construct_dataarray(dim_num, dtype, contains_nan=True, dask=dask)
da = construct_dataarray(dim_num, dtype, contains_nan=contains_nan, dask=dask)
min_count = 3

actual = getattr(da, func)(dim=aggdim, skipna=True, min_count=min_count)
expected = series_reduce(da, func, skipna=True, dim=aggdim, min_count=min_count)
actual = getattr(da, func)(dim=aggdim, skipna=skipna, min_count=min_count)
expected = series_reduce(da, func, skipna=skipna, dim=aggdim, min_count=min_count)
assert_allclose(actual, expected)
assert_dask_array(actual, dask)

Expand Down

0 comments on commit 333e8db

Please sign in to comment.