Skip to content

Commit 8676f16

Browse files
committed
Allow mean with time dtypes
Closes pydata#5897 Closes pydata#6995
1 parent 25debff commit 8676f16

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

xarray/core/_aggregations.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,6 @@ def mean(
468468
:ref:`agg`
469469
User guide on reduction or aggregation operations.
470470
471-
Notes
472-
-----
473-
Non-numeric variables will be removed prior to reducing.
474-
475471
Examples
476472
--------
477473
>>> da = xr.DataArray(
@@ -510,7 +506,7 @@ def mean(
510506
duck_array_ops.mean,
511507
dim=dim,
512508
skipna=skipna,
513-
numeric_only=True,
509+
numeric_only=False,
514510
keep_attrs=keep_attrs,
515511
**kwargs,
516512
)
@@ -1630,10 +1626,6 @@ def mean(
16301626
:ref:`agg`
16311627
User guide on reduction or aggregation operations.
16321628
1633-
Notes
1634-
-----
1635-
Non-numeric variables will be removed prior to reducing.
1636-
16371629
Examples
16381630
--------
16391631
>>> da = xr.DataArray(
@@ -2903,8 +2895,6 @@ def mean(
29032895
Pass flox-specific keyword arguments in ``**kwargs``.
29042896
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
29052897
2906-
Non-numeric variables will be removed prior to reducing.
2907-
29082898
Examples
29092899
--------
29102900
>>> da = xr.DataArray(
@@ -2952,7 +2942,7 @@ def mean(
29522942
func="mean",
29532943
dim=dim,
29542944
skipna=skipna,
2955-
numeric_only=True,
2945+
numeric_only=False,
29562946
# fill_value=fill_value,
29572947
keep_attrs=keep_attrs,
29582948
**kwargs,
@@ -2962,7 +2952,7 @@ def mean(
29622952
duck_array_ops.mean,
29632953
dim=dim,
29642954
skipna=skipna,
2965-
numeric_only=True,
2955+
numeric_only=False,
29662956
keep_attrs=keep_attrs,
29672957
**kwargs,
29682958
)
@@ -4391,8 +4381,6 @@ def mean(
43914381
Pass flox-specific keyword arguments in ``**kwargs``.
43924382
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
43934383
4394-
Non-numeric variables will be removed prior to reducing.
4395-
43964384
Examples
43974385
--------
43984386
>>> da = xr.DataArray(
@@ -4440,7 +4428,7 @@ def mean(
44404428
func="mean",
44414429
dim=dim,
44424430
skipna=skipna,
4443-
numeric_only=True,
4431+
numeric_only=False,
44444432
# fill_value=fill_value,
44454433
keep_attrs=keep_attrs,
44464434
**kwargs,
@@ -4450,7 +4438,7 @@ def mean(
44504438
duck_array_ops.mean,
44514439
dim=dim,
44524440
skipna=skipna,
4453-
numeric_only=True,
4441+
numeric_only=False,
44544442
keep_attrs=keep_attrs,
44554443
**kwargs,
44564444
)
@@ -5840,8 +5828,6 @@ def mean(
58405828
Pass flox-specific keyword arguments in ``**kwargs``.
58415829
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
58425830
5843-
Non-numeric variables will be removed prior to reducing.
5844-
58455831
Examples
58465832
--------
58475833
>>> da = xr.DataArray(
@@ -7220,8 +7206,6 @@ def mean(
72207206
Pass flox-specific keyword arguments in ``**kwargs``.
72217207
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
72227208
7223-
Non-numeric variables will be removed prior to reducing.
7224-
72257209
Examples
72267210
--------
72277211
>>> da = xr.DataArray(

xarray/namedarray/_aggregations.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,6 @@ def mean(
363363
:ref:`agg`
364364
User guide on reduction or aggregation operations.
365365
366-
Notes
367-
-----
368-
Non-numeric variables will be removed prior to reducing.
369-
370366
Examples
371367
--------
372368
>>> from xarray.namedarray.core import NamedArray

xarray/tests/test_groupby.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
assert_identical,
2929
create_test_data,
3030
has_cftime,
31+
has_dask,
3132
has_flox,
3233
has_pandas_ge_2_2,
3334
requires_cftime,
@@ -2788,3 +2789,43 @@ def test_multiple_groupers_mixed(use_flox) -> None:
27882789
# 1. lambda x: x
27892790
# 2. grouped-reduce on unique coords is identical to array
27902791
# 3. group_over == groupby-reduce along other dimensions
2792+
2793+
2794+
# Put your MCVE code here
2795+
import xarray as xr
2796+
2797+
2798+
@pytest.mark.parametrize(
2799+
"chunk",
2800+
[
2801+
pytest.param(
2802+
True, marks=pytest.mark.skipif(not has_dask, reason="requires dask")
2803+
),
2804+
False,
2805+
],
2806+
)
2807+
@pytest.mark.parametrize(
2808+
"use_cftime",
2809+
[
2810+
pytest.param(
2811+
True, marks=pytest.mark.skipif(not has_cftime, reason="requires cftime")
2812+
),
2813+
False,
2814+
],
2815+
)
2816+
def test_datetime_mean(chunk, use_cftime):
2817+
ds = xr.Dataset(
2818+
{
2819+
"var1": (
2820+
("time",),
2821+
xr.date_range(
2822+
"2021-10-31", periods=10, freq="D", use_cftime=use_cftime
2823+
),
2824+
),
2825+
"var2": (("x",), list(range(10))),
2826+
}
2827+
)
2828+
if chunk:
2829+
ds = ds.chunk()
2830+
assert "var1" in ds.groupby("x").mean("time")
2831+
assert "var1" in ds.mean("x")

xarray/util/generate_aggregations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
2525
from __future__ import annotations
2626
27-
from collections.abc import Sequence
28-
from typing import TYPE_CHECKING, Any, Callable
27+
from collections.abc import Callable, Sequence
28+
from typing import TYPE_CHECKING, Any
2929
3030
from xarray.core import duck_array_ops
3131
from xarray.core.options import OPTIONS
@@ -496,7 +496,7 @@ def generate_code(self, method, has_keep_attrs):
496496
Method("any", bool_reduce=True),
497497
Method("max", extra_kwargs=(skipna,)),
498498
Method("min", extra_kwargs=(skipna,)),
499-
Method("mean", extra_kwargs=(skipna,), numeric_only=True),
499+
Method("mean", extra_kwargs=(skipna,), numeric_only=False),
500500
Method("prod", extra_kwargs=(skipna, min_count), numeric_only=True),
501501
Method("sum", extra_kwargs=(skipna, min_count), numeric_only=True),
502502
Method("std", extra_kwargs=(skipna, ddof), numeric_only=True),

0 commit comments

Comments
 (0)