Skip to content

Skip mean over empty axis #5207

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

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Breaking changes
as positional, all others need to be passed are keyword arguments. This is part of the
refactor to support external backends (:issue:`4309`, :pull:`4989`).
By `Alessandro Amici <https://github.com/alexamici>`_.
- :py:func:`mean` does not change the data if axis is None. This
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:py:func:mean is actually a method (there should be other references in the whats-new which can be copied)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found an example, so I have removed the explicit reference

ensures that Datasets where some variables do not have the averaged
dimensions are not accidentially changed (:issue:`4885`).
By `David Schwörer <https://github.com/dschwoerer>`_

Deprecations
~~~~~~~~~~~~
Expand Down
5 changes: 5 additions & 0 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ def mean(array, axis=None, skipna=None, **kwargs):
dtypes"""
from .common import _contains_cftime_datetimes

# The mean over an empty axis shouldn't change the data
# See https://github.com/pydata/xarray/issues/4885
if axis == tuple():
return array

array = asarray(array)
if array.dtype.kind in "Mm":
offset = _datetime_nanmin(array)
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ def test_cftime_datetime_mean_dask_error():
da.mean()


def test_mean_dtype():
ds = Dataset()
ds["pos"] = [1, 2, 3]
ds["data"] = ("pos", "time"), [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
ds["var"] = "pos", [2, 3, 4]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI you can construct the dataset in a single expression, but it's also fine as-is (Dataset(dict(pos=..., data=...)))

ds2 = ds.mean(dim="time")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convention:

Suggested change
ds2 = ds.mean(dim="time")
result = ds.mean(dim="time")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not store the result but use it directly. Is that ok as well?

assert all(ds2["var"] == ds["var"])
assert ds2["var"].dtype == ds["var"].dtype


@pytest.mark.parametrize("dim_num", [1, 2])
@pytest.mark.parametrize("dtype", [float, int, np.float32, np.bool_])
@pytest.mark.parametrize("dask", [False, True])
Expand Down