Skip to content
forked from pydata/xarray

Commit 9ffe4b0

Browse files
committed
Allow mean with time dtypes
Closes pydata#5897 Closes pydata#6995 Closes pydata#10217
1 parent eb2ff69 commit 9ffe4b0

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

xarray/core/_aggregations.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -1776,10 +1776,6 @@ def mean(
17761776
:ref:`agg`
17771777
User guide on reduction or aggregation operations.
17781778
1779-
Notes
1780-
-----
1781-
Non-numeric variables will be removed prior to reducing.
1782-
17831779
Examples
17841780
--------
17851781
>>> da = xr.DataArray(
@@ -1818,7 +1814,7 @@ def mean(
18181814
duck_array_ops.mean,
18191815
dim=dim,
18201816
skipna=skipna,
1821-
numeric_only=True,
1817+
numeric_only=False,
18221818
keep_attrs=keep_attrs,
18231819
**kwargs,
18241820
)
@@ -2948,10 +2944,6 @@ def mean(
29482944
:ref:`agg`
29492945
User guide on reduction or aggregation operations.
29502946
2951-
Notes
2952-
-----
2953-
Non-numeric variables will be removed prior to reducing.
2954-
29552947
Examples
29562948
--------
29572949
>>> da = xr.DataArray(
@@ -4231,8 +4223,6 @@ def mean(
42314223
Pass flox-specific keyword arguments in ``**kwargs``.
42324224
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
42334225
4234-
Non-numeric variables will be removed prior to reducing.
4235-
42364226
Examples
42374227
--------
42384228
>>> da = xr.DataArray(
@@ -4280,7 +4270,7 @@ def mean(
42804270
func="mean",
42814271
dim=dim,
42824272
skipna=skipna,
4283-
numeric_only=True,
4273+
numeric_only=False,
42844274
# fill_value=fill_value,
42854275
keep_attrs=keep_attrs,
42864276
**kwargs,
@@ -4290,7 +4280,7 @@ def mean(
42904280
duck_array_ops.mean,
42914281
dim=dim,
42924282
skipna=skipna,
4293-
numeric_only=True,
4283+
numeric_only=False,
42944284
keep_attrs=keep_attrs,
42954285
**kwargs,
42964286
)
@@ -5729,8 +5719,6 @@ def mean(
57295719
Pass flox-specific keyword arguments in ``**kwargs``.
57305720
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
57315721
5732-
Non-numeric variables will be removed prior to reducing.
5733-
57345722
Examples
57355723
--------
57365724
>>> da = xr.DataArray(
@@ -5778,7 +5766,7 @@ def mean(
57785766
func="mean",
57795767
dim=dim,
57805768
skipna=skipna,
5781-
numeric_only=True,
5769+
numeric_only=False,
57825770
# fill_value=fill_value,
57835771
keep_attrs=keep_attrs,
57845772
**kwargs,
@@ -5788,7 +5776,7 @@ def mean(
57885776
duck_array_ops.mean,
57895777
dim=dim,
57905778
skipna=skipna,
5791-
numeric_only=True,
5779+
numeric_only=False,
57925780
keep_attrs=keep_attrs,
57935781
**kwargs,
57945782
)
@@ -7188,8 +7176,6 @@ def mean(
71887176
Pass flox-specific keyword arguments in ``**kwargs``.
71897177
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
71907178
7191-
Non-numeric variables will be removed prior to reducing.
7192-
71937179
Examples
71947180
--------
71957181
>>> da = xr.DataArray(
@@ -8578,8 +8564,6 @@ def mean(
85788564
Pass flox-specific keyword arguments in ``**kwargs``.
85798565
See the `flox documentation <https://flox.readthedocs.io>`_ for more.
85808566
8581-
Non-numeric variables will be removed prior to reducing.
8582-
85838567
Examples
85848568
--------
85858569
>>> da = xr.DataArray(

xarray/namedarray/_aggregations.py

-4
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,6 @@ def mean(
352352
:ref:`agg`
353353
User guide on reduction or aggregation operations.
354354
355-
Notes
356-
-----
357-
Non-numeric variables will be removed prior to reducing.
358-
359355
Examples
360356
--------
361357
>>> from xarray.namedarray.core import NamedArray

xarray/tests/test_groupby.py

+27
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,33 @@ def test_groupby_dask_eager_load_warnings() -> None:
32923292
ds.groupby_bins("x", bins=[1, 2, 3], eagerly_compute_group=False)
32933293

32943294

3295+
@pytest.mark.parametrize(
3296+
"chunk",
3297+
[
3298+
pytest.param(
3299+
True, marks=pytest.mark.skipif(not has_dask, reason="requires dask")
3300+
),
3301+
False,
3302+
],
3303+
)
3304+
def test_datetime_mean(chunk, use_cftime):
3305+
ds = xr.Dataset(
3306+
{
3307+
"var1": (
3308+
("time",),
3309+
xr.date_range(
3310+
"2021-10-31", periods=10, freq="D", use_cftime=use_cftime
3311+
),
3312+
),
3313+
"var2": (("x",), list(range(10))),
3314+
}
3315+
)
3316+
if chunk:
3317+
ds = ds.chunk()
3318+
assert "var1" in ds.groupby("x").mean("time")
3319+
assert "var1" in ds.mean("x")
3320+
3321+
32953322
# TODO: Possible property tests to add to this module
32963323
# 1. lambda x: x
32973324
# 2. grouped-reduce on unique coords is identical to array

xarray/util/generate_aggregations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def generate_code(self, method, has_keep_attrs):
515515
Method("any", bool_reduce=True),
516516
Method("max", extra_kwargs=(skipna,)),
517517
Method("min", extra_kwargs=(skipna,)),
518-
Method("mean", extra_kwargs=(skipna,), numeric_only=True),
518+
Method("mean", extra_kwargs=(skipna,), numeric_only=False),
519519
Method("prod", extra_kwargs=(skipna, min_count), numeric_only=True),
520520
Method("sum", extra_kwargs=(skipna, min_count), numeric_only=True),
521521
Method("std", extra_kwargs=(skipna, ddof), numeric_only=True),

0 commit comments

Comments
 (0)