-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
What is the correct way to check that something is array-like? #8701
Comments
i don't believe JAX That said, the most permissive way for libraries to handle this, I think, would be to call If you want something a bit more restrictive but that would still accept explicitly array-like objects that aren't instances of def is_arraylike(x):
return hasattr(x, '__array__') or hasattr(x, '__array_interface__') |
@jakevdp Thank you. I only spent about 15 minutes on this, but when I updated to Jax master, I started getting crashes in pandas/seaborn that were fixed by converting to numpy array. It's possible that certain operations that would previously have forced a numpy array are now supported by Jax, and so arrays remain Jax arrays longer than they used to? If I run into this again, I'll submit an issue to them with a link here. |
One recent change that may be relevant is that iteration over jax arrays now returns jax arrays, where it used to return numpy arrays. So if you had code like this: import jax.numpy as jnp
import numpy as np
x = list(jnp.ones((2, 3)))
isinstance(x[0], np.ndarray) it would previously have returned True, and now returns False (see #8043). |
Good thinking. That could be it. When I looked at their code, they were doing things like iterating and checking types. That's probably their fallback when the object is not an |
I guess that the new version of Jax has it so that
jax.numpy.ndarray
no longer inherits fromnp.ndarray
, which is great. Unfortunately, other packages like Seaborn and Pandas are relying onisinstance(x, np.ndarray)
in a few places. This causes their behavior to change, and forces me to cast tonp.ndarray
.I was just wondering, what should they be doing instead? Is it looking for the array interface? But
jax.numpy.ndarray
doesn't expose that. If it's something else, I'll post the appropriate issues on their trackers. If there's no way to check yet, that would be good to know.Thanks.
The text was updated successfully, but these errors were encountered: