Skip to content

Commit 3a6cdaf

Browse files
committed
catch sparse error a bit more elegantly
1 parent 785f038 commit 3a6cdaf

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

xarray/core/duck_array_ops.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import contextlib
77
import inspect
88
import warnings
9+
from distutils.version import LooseVersion
910
from functools import partial
1011

1112
import numpy as np
@@ -151,29 +152,24 @@ def trapz(y, x, axis):
151152

152153
def astype(data, **kwargs):
153154
try:
154-
return data.astype(**kwargs)
155-
except TypeError as e:
156-
# FIXME: This should no longer be necessary in future versions of sparse
157-
# Current versions of sparse (v0.10.0) don't support the "casting" kwarg
158-
# This was fixed by https://github.com/pydata/sparse/pull/392
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-
)
173-
kwargs.pop("casting")
174-
else:
175-
raise e
176-
return data.astype(**kwargs)
155+
import sparse
156+
except ImportError:
157+
sparse = None
158+
159+
if (
160+
sparse is not None
161+
and isinstance(data, sparse._coo.core.COO)
162+
and LooseVersion(sparse.__version__) < LooseVersion("0.11.0")
163+
and "casting" in kwargs
164+
):
165+
warnings.warn(
166+
"The current version of sparse does not support the 'casting' argument. It will be ignored in the call to astype().",
167+
RuntimeWarning,
168+
stacklevel=4,
169+
)
170+
kwargs.pop("casting")
171+
172+
return data.astype(**kwargs)
177173

178174

179175
def asarray(data, xp=np):

0 commit comments

Comments
 (0)