From 562015ce7063a634c2072f9c84a724fecf18aff0 Mon Sep 17 00:00:00 2001 From: Tom White Date: Sun, 11 Aug 2024 19:15:04 +0100 Subject: [PATCH] Use duck array ops for `around` and `round` (#9326) * Use duck array ops for `around` and `round` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add type hint to `around` * Update xarray/coding/variables.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/coding/variables.py | 4 ++-- xarray/core/duck_array_ops.py | 42 ++++++++--------------------------- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/xarray/coding/variables.py b/xarray/coding/variables.py index 8a3afe650f2..c240cfe5939 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.round(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..4e6b066591f 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -12,13 +12,14 @@ import warnings from functools import partial from importlib import import_module +from typing import Callable import numpy as np 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 - around, # noqa full_like, gradient, isclose, @@ -29,7 +30,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 @@ -122,37 +122,13 @@ 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: Callable = round def isnull(data):