Skip to content

Commit 333e8db

Browse files
mathausedcheriankeewis
authored
fix: min_counts for non-nan sum and prod (#4423)
* 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>
1 parent 5f8ddfe commit 333e8db

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ Bug fixes
3939
- Fix silently overwriting the `engine` key when passing :py:func:`open_dataset` a file object
4040
to an incompatible netCDF (:issue:`4457`). Now incompatible combinations of files and engines raise
4141
an exception instead. By `Alessandro Amici <https://github.com/alexamici>`_.
42-
42+
- The ``min_count`` argument to :py:meth:`DataArray.sum()` and :py:meth:`DataArray.prod()`
43+
is now ignored when not applicable, i.e. when ``skipna=False`` or when ``skipna=None``
44+
and the dtype does not have a missing value (:issue:`4352`).
45+
By `Mathias Hauser <https://github.com/mathause>`_.
4346

4447
Documentation
4548
~~~~~~~~~~~~~

xarray/core/duck_array_ops.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ def f(values, axis=None, skipna=None, **kwargs):
325325
nanname = "nan" + name
326326
func = getattr(nanops, nanname)
327327
else:
328+
if name in ["sum", "prod"]:
329+
kwargs.pop("min_count", None)
328330
func = _dask_or_eager_func(name, dask_module=dask_module)
329331

330332
try:
@@ -361,7 +363,7 @@ def f(values, axis=None, skipna=None, **kwargs):
361363
median.numeric_only = True
362364
prod = _create_nan_agg_method("prod")
363365
prod.numeric_only = True
364-
sum.available_min_count = True
366+
prod.available_min_count = True
365367
cumprod_1d = _create_nan_agg_method("cumprod")
366368
cumprod_1d.numeric_only = True
367369
cumsum_1d = _create_nan_agg_method("cumsum")

xarray/tests/test_duck_array_ops.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,17 @@ def test_dask_gradient(axis, edge_order):
580580
@pytest.mark.parametrize("dask", [False, True])
581581
@pytest.mark.parametrize("func", ["sum", "prod"])
582582
@pytest.mark.parametrize("aggdim", [None, "x"])
583-
def test_min_count(dim_num, dtype, dask, func, aggdim):
583+
@pytest.mark.parametrize("contains_nan", [True, False])
584+
@pytest.mark.parametrize("skipna", [True, False, None])
585+
def test_min_count(dim_num, dtype, dask, func, aggdim, contains_nan, skipna):
584586
if dask and not has_dask:
585587
pytest.skip("requires dask")
586588

587-
da = construct_dataarray(dim_num, dtype, contains_nan=True, dask=dask)
589+
da = construct_dataarray(dim_num, dtype, contains_nan=contains_nan, dask=dask)
588590
min_count = 3
589591

590-
actual = getattr(da, func)(dim=aggdim, skipna=True, min_count=min_count)
591-
expected = series_reduce(da, func, skipna=True, dim=aggdim, min_count=min_count)
592+
actual = getattr(da, func)(dim=aggdim, skipna=skipna, min_count=min_count)
593+
expected = series_reduce(da, func, skipna=skipna, dim=aggdim, min_count=min_count)
592594
assert_allclose(actual, expected)
593595
assert_dask_array(actual, dask)
594596

0 commit comments

Comments
 (0)