Skip to content

Commit

Permalink
Use duck array ops for around and round (#9326)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2024
1 parent 4bae53c commit 562015c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 35 deletions.
4 changes: 2 additions & 2 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 9 additions & 33 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 562015c

Please sign in to comment.