-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
explicitly cast the dtype of where
's condition parameter to bool
#10087
base: main
Are you sure you want to change the base?
Conversation
xarray/core/duck_array_ops.py
Outdated
@@ -373,7 +373,7 @@ def sum_where(data, axis=None, dtype=None, where=None): | |||
def where(condition, x, y): | |||
"""Three argument where() with better dtype promotion rules.""" | |||
xp = get_array_namespace(condition, x, y) | |||
return xp.where(condition, *as_shared_dtype([x, y], xp=xp)) | |||
return xp.where(xp.astype(condition, xp.bool), *as_shared_dtype([x, y], xp=xp)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like it is not that simple... at some point we're trying to call
duck_array_ops.where(True, np.arange(5), np.float64(np.nan))
where numpy
implements astype
by forwarding to the ndarray
method (and our own wrapper does the same).
To resolve this, we probably need to make cond
a 0d array:
return xp.where(xp.astype(condition, xp.bool), *as_shared_dtype([x, y], xp=xp)) | |
try: | |
cond_ = astype(condition, xp.bool) | |
except AttributeError: | |
cond_ = xp.asarray(condition, dtype=xp.bool) | |
return xp.where(cond_, *as_shared_dtype([x, y], xp=xp)) |
however, I'm not sure I like the way this looks. Another option would be to make astype
work for this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @keewis ! I got stuck on the same thing. What I did:
# Check array type
if isinstance(condition, np.ndarray):
condition = condition.astype(dtype=np.bool)
elif isinstance(condition, xp_strict._array_object.Array):
condition = xp_strict.asarray(condition, dtype=xp_strict.bool)
else:
raise TypeError("Unsupported array type")
xp = get_array_namespace(condition, x, y)
return xp.where(condition, *as_shared_dtype([x, y], xp=xp))
This passes test_array_api
but test_computation
fails with a bunch of TypeErrors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, that gave me some ideas on how to possibly fix this in a cleaner way (and I also found a potential bug in the groupby code)
@@ -524,7 +524,7 @@ def factorize(self) -> EncodedGroups: | |||
# Restore these after the raveling | |||
broadcasted_masks = broadcast(*masks) | |||
mask = functools.reduce(np.logical_or, broadcasted_masks) # type: ignore[arg-type] | |||
_flatcodes = where(mask, -1, _flatcodes) | |||
_flatcodes = where(mask.data, -1, _flatcodes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
Until pydata#10087 is ready
(this is necessary because
array-api-strict
raises an error otherwise)