From 4794f51af2f3126d0109e54bbe0f0ffe8eed83c3 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 8 Aug 2024 10:21:56 +0100 Subject: [PATCH 1/4] Use duck array ops for `around` and `round` --- xarray/coding/variables.py | 4 ++-- xarray/core/duck_array_ops.py | 37 +++++------------------------------ 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/xarray/coding/variables.py b/xarray/coding/variables.py index 8a3afe650f2..925efa73a65 100644 --- a/xarray/coding/variables.py +++ b/xarray/coding/variables.py @@ -662,8 +662,8 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable: SerializationWarning, stacklevel=10, ) - data = np.around(data) - data = data.astype(dtype=dtype) + data = duck_array_ops.around(data) + data = duck_array_ops.astype(data, dtype=dtype) return Variable(dims, data, attrs, encoding, fastpath=True) else: return variable diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index 8993c136ba6..b33672482bb 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -18,7 +18,6 @@ from numpy import all as array_all # noqa from numpy import any as array_any # noqa from numpy import ( # noqa - around, # noqa full_like, gradient, isclose, @@ -122,37 +121,11 @@ def fail_on_dask_array_input(values, msg=None, func_name=None): # Requires special-casing because pandas won't automatically dispatch to dask.isnull via NEP-18 pandas_isnull = _dask_or_eager_func("isnull", eager_module=pd, dask_module="dask.array") -# np.around has failing doctests, overwrite it so they pass: -# https://github.com/numpy/numpy/issues/19759 -around.__doc__ = str.replace( - around.__doc__ or "", - "array([0., 2.])", - "array([0., 2.])", -) -around.__doc__ = str.replace( - around.__doc__ or "", - "array([0., 2.])", - "array([0., 2.])", -) -around.__doc__ = str.replace( - around.__doc__ or "", - "array([0.4, 1.6])", - "array([0.4, 1.6])", -) -around.__doc__ = str.replace( - around.__doc__ or "", - "array([0., 2., 2., 4., 4.])", - "array([0., 2., 2., 4., 4.])", -) -around.__doc__ = str.replace( - around.__doc__ or "", - ( - ' .. [2] "How Futile are Mindless Assessments of\n' - ' Roundoff in Floating-Point Computation?", William Kahan,\n' - " https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf\n" - ), - "", -) +def round(array): + xp = get_array_namespace(array) + return xp.round(array) + +around = round def isnull(data): From 5e05f887d3cb9f960f083ad4af118f257944f153 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:05:19 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/core/duck_array_ops.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index b33672482bb..ae4c7b3e67e 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -17,6 +17,7 @@ import pandas as pd from numpy import all as array_all # noqa from numpy import any as array_any # noqa +from numpy import concatenate as _concatenate from numpy import ( # noqa full_like, gradient, @@ -28,7 +29,6 @@ transpose, unravel_index, ) -from numpy import concatenate as _concatenate from numpy.lib.stride_tricks import sliding_window_view # noqa from packaging.version import Version from pandas.api.types import is_extension_array_dtype @@ -121,10 +121,12 @@ def fail_on_dask_array_input(values, msg=None, func_name=None): # Requires special-casing because pandas won't automatically dispatch to dask.isnull via NEP-18 pandas_isnull = _dask_or_eager_func("isnull", eager_module=pd, dask_module="dask.array") + def round(array): xp = get_array_namespace(array) return xp.round(array) + around = round From 7e51f36781b39a7848ae93f982c92ee28d80bc47 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 8 Aug 2024 15:35:20 +0100 Subject: [PATCH 3/4] Add type hint to `around` --- xarray/core/duck_array_ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index ae4c7b3e67e..4e6b066591f 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -12,6 +12,7 @@ import warnings from functools import partial from importlib import import_module +from typing import Callable import numpy as np import pandas as pd @@ -127,7 +128,7 @@ def round(array): return xp.round(array) -around = round +around: Callable = round def isnull(data): From d8ef62295a193e2e7cf78613099627651e348c0c Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:01:44 +0200 Subject: [PATCH 4/4] Update xarray/coding/variables.py --- xarray/coding/variables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/coding/variables.py b/xarray/coding/variables.py index 925efa73a65..c240cfe5939 100644 --- a/xarray/coding/variables.py +++ b/xarray/coding/variables.py @@ -662,7 +662,7 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable: SerializationWarning, stacklevel=10, ) - data = duck_array_ops.around(data) + data = duck_array_ops.round(data) data = duck_array_ops.astype(data, dtype=dtype) return Variable(dims, data, attrs, encoding, fastpath=True) else: