Skip to content

Commit 562015c

Browse files
tomwhitepre-commit-ci[bot]Illviljan
authored
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 <[email protected]>
1 parent 4bae53c commit 562015c

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

xarray/coding/variables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
662662
SerializationWarning,
663663
stacklevel=10,
664664
)
665-
data = np.around(data)
666-
data = data.astype(dtype=dtype)
665+
data = duck_array_ops.round(data)
666+
data = duck_array_ops.astype(data, dtype=dtype)
667667
return Variable(dims, data, attrs, encoding, fastpath=True)
668668
else:
669669
return variable

xarray/core/duck_array_ops.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import warnings
1313
from functools import partial
1414
from importlib import import_module
15+
from typing import Callable
1516

1617
import numpy as np
1718
import pandas as pd
1819
from numpy import all as array_all # noqa
1920
from numpy import any as array_any # noqa
21+
from numpy import concatenate as _concatenate
2022
from numpy import ( # noqa
21-
around, # noqa
2223
full_like,
2324
gradient,
2425
isclose,
@@ -29,7 +30,6 @@
2930
transpose,
3031
unravel_index,
3132
)
32-
from numpy import concatenate as _concatenate
3333
from numpy.lib.stride_tricks import sliding_window_view # noqa
3434
from packaging.version import Version
3535
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):
122122
# Requires special-casing because pandas won't automatically dispatch to dask.isnull via NEP-18
123123
pandas_isnull = _dask_or_eager_func("isnull", eager_module=pd, dask_module="dask.array")
124124

125-
# np.around has failing doctests, overwrite it so they pass:
126-
# https://github.com/numpy/numpy/issues/19759
127-
around.__doc__ = str.replace(
128-
around.__doc__ or "",
129-
"array([0., 2.])",
130-
"array([0., 2.])",
131-
)
132-
around.__doc__ = str.replace(
133-
around.__doc__ or "",
134-
"array([0., 2.])",
135-
"array([0., 2.])",
136-
)
137-
around.__doc__ = str.replace(
138-
around.__doc__ or "",
139-
"array([0.4, 1.6])",
140-
"array([0.4, 1.6])",
141-
)
142-
around.__doc__ = str.replace(
143-
around.__doc__ or "",
144-
"array([0., 2., 2., 4., 4.])",
145-
"array([0., 2., 2., 4., 4.])",
146-
)
147-
around.__doc__ = str.replace(
148-
around.__doc__ or "",
149-
(
150-
' .. [2] "How Futile are Mindless Assessments of\n'
151-
' Roundoff in Floating-Point Computation?", William Kahan,\n'
152-
" https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf\n"
153-
),
154-
"",
155-
)
125+
126+
def round(array):
127+
xp = get_array_namespace(array)
128+
return xp.round(array)
129+
130+
131+
around: Callable = round
156132

157133

158134
def isnull(data):

0 commit comments

Comments
 (0)