Skip to content

Allow mean with time dtypes #10227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions xarray/core/_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,10 +1776,6 @@ def mean(
:ref:`agg`
User guide on reduction or aggregation operations.

Notes
-----
Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down Expand Up @@ -1818,7 +1814,7 @@ def mean(
duck_array_ops.mean,
dim=dim,
skipna=skipna,
numeric_only=True,
numeric_only=False,
keep_attrs=keep_attrs,
**kwargs,
)
Expand Down Expand Up @@ -2948,10 +2944,6 @@ def mean(
:ref:`agg`
User guide on reduction or aggregation operations.

Notes
-----
Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down Expand Up @@ -4231,8 +4223,6 @@ def mean(
Pass flox-specific keyword arguments in ``**kwargs``.
See the `flox documentation <https://flox.readthedocs.io>`_ for more.

Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down Expand Up @@ -4280,7 +4270,7 @@ def mean(
func="mean",
dim=dim,
skipna=skipna,
numeric_only=True,
numeric_only=False,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
Expand All @@ -4290,7 +4280,7 @@ def mean(
duck_array_ops.mean,
dim=dim,
skipna=skipna,
numeric_only=True,
numeric_only=False,
keep_attrs=keep_attrs,
**kwargs,
)
Expand Down Expand Up @@ -5729,8 +5719,6 @@ def mean(
Pass flox-specific keyword arguments in ``**kwargs``.
See the `flox documentation <https://flox.readthedocs.io>`_ for more.

Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down Expand Up @@ -5778,7 +5766,7 @@ def mean(
func="mean",
dim=dim,
skipna=skipna,
numeric_only=True,
numeric_only=False,
# fill_value=fill_value,
keep_attrs=keep_attrs,
**kwargs,
Expand All @@ -5788,7 +5776,7 @@ def mean(
duck_array_ops.mean,
dim=dim,
skipna=skipna,
numeric_only=True,
numeric_only=False,
keep_attrs=keep_attrs,
**kwargs,
)
Expand Down Expand Up @@ -7188,8 +7176,6 @@ def mean(
Pass flox-specific keyword arguments in ``**kwargs``.
See the `flox documentation <https://flox.readthedocs.io>`_ for more.

Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down Expand Up @@ -8578,8 +8564,6 @@ def mean(
Pass flox-specific keyword arguments in ``**kwargs``.
See the `flox documentation <https://flox.readthedocs.io>`_ for more.

Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> da = xr.DataArray(
Expand Down
4 changes: 0 additions & 4 deletions xarray/namedarray/_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@ def mean(
:ref:`agg`
User guide on reduction or aggregation operations.

Notes
-----
Non-numeric variables will be removed prior to reducing.

Examples
--------
>>> from xarray.namedarray.core import NamedArray
Expand Down
27 changes: 27 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@
if letters_as_coord:
data = data.set_coords("letters")

expected = data.mean("y")

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12 all-but-dask

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12 all-but-dask

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12 all-but-dask

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12 all-but-dask

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 min-all-deps

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 min-all-deps

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 min-all-deps

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 min-all-deps

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U1'), dtype('<U1')) -> None

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13 all-but-numba

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13 all-but-numba

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13 all-but-numba

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13 all-but-numba

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-string-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-string-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-True] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))

Check failure on line 908 in xarray/tests/test_groupby.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.13

test_groupby_dataset_reduce_ellipsis[False-group-by-unique-grouper-False] TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U1'), dtype('<U1'), dtype('<U2'))
expected["yonly"] = expected["yonly"].variable.set_dims({"x": 3})
gb = data.groupby(by_func("x"))
with xr.set_options(use_flox=use_flox):
Expand Down Expand Up @@ -3292,6 +3292,33 @@
ds.groupby_bins("x", bins=[1, 2, 3], eagerly_compute_group=False)


@pytest.mark.parametrize(
"chunk",
[
pytest.param(
True, marks=pytest.mark.skipif(not has_dask, reason="requires dask")
),
False,
],
)
def test_datetime_mean(chunk, use_cftime):
ds = xr.Dataset(
{
"var1": (
("time",),
xr.date_range(
"2021-10-31", periods=10, freq="D", use_cftime=use_cftime
),
),
"var2": (("x",), list(range(10))),
}
)
if chunk:
ds = ds.chunk()
assert "var1" in ds.groupby("x").mean("time")
assert "var1" in ds.mean("x")


# TODO: Possible property tests to add to this module
# 1. lambda x: x
# 2. grouped-reduce on unique coords is identical to array
Expand Down
2 changes: 1 addition & 1 deletion xarray/util/generate_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def generate_code(self, method, has_keep_attrs):
Method("any", bool_reduce=True),
Method("max", extra_kwargs=(skipna,)),
Method("min", extra_kwargs=(skipna,)),
Method("mean", extra_kwargs=(skipna,), numeric_only=True),
Method("mean", extra_kwargs=(skipna,), numeric_only=False),
Method("prod", extra_kwargs=(skipna, min_count), numeric_only=True),
Method("sum", extra_kwargs=(skipna, min_count), numeric_only=True),
Method("std", extra_kwargs=(skipna, ddof), numeric_only=True),
Expand Down
Loading