Skip to content

Commit 9407894

Browse files
committed
catch sparse error a bit more elegantly
1 parent 2206c31 commit 9407894

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
@@ -7,6 +7,7 @@
77
import datetime
88
import inspect
99
import warnings
10+
from distutils.version import LooseVersion
1011
from functools import partial
1112

1213
import numpy as np
@@ -152,29 +153,24 @@ def trapz(y, x, axis):
152153

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

179175

180176
def asarray(data, xp=np):

0 commit comments

Comments
 (0)