Skip to content

Commit 612acef

Browse files
dnowacki-usgskeewis
authored andcommitted
improve error handling and check if sparse array
1 parent be96c7f commit 612acef

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

xarray/core/duck_array_ops.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,23 @@ def astype(data, **kwargs):
156156
# FIXME: This should no longer be necessary in future versions of sparse
157157
# Current versions of sparse (v0.10.0) don't support the "casting" kwarg
158158
# This was fixed by https://github.com/pydata/sparse/pull/392
159-
if "casting" in repr(e):
159+
try:
160+
import sparse
161+
except ImportError:
162+
sparse = None
163+
if (
164+
"got an unexpected keyword argument 'casting'" in repr(e)
165+
and sparse is not None
166+
and isinstance(data, sparse._coo.core.COO)
167+
):
168+
warnings.warn(
169+
"The current version of sparse does not support the 'casting' argument. It will be ignored in the call to astype().",
170+
RuntimeWarning,
171+
stacklevel=4,
172+
)
160173
kwargs.pop("casting")
174+
else:
175+
raise e
161176
return data.astype(**kwargs)
162177

163178

0 commit comments

Comments
 (0)