-
Notifications
You must be signed in to change notification settings - Fork 193
Add support for 'arraylike' objects as JSON arrays #317
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
base: develop
Are you sure you want to change the base?
Conversation
Co-authored-by: Dobatymo <[email protected]>
|
|
||
|
|
||
| def is_array(arg): | ||
| return hasattr(arg, "__array__") and arg.shape != () |
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.
Worth linking to https://numpy.org/doc/stable/user/basics.interoperability.html#the-array-method in a comment?
Also, is it guaranteed that an object with __array__ will always have a shape attribute defined?
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.
The numpy docs describes that __array__ method if it exists should always return a np.ndarray instance (ideally with zero copy) of which that type always has a shape attribute (tested with dask and astropy).
__array_interface__ is a bit more array library agnostic and explicitly documented to require shape, but __array__ is already being used so this project doesn't need to explicitly import numpy to perform np.array(value.__array_interface__, copy=False).
JMESPath.py is limited in that only the
dictandlistderived containers returned by the built-injsonlibrary are supported in the object hierarchy due to the use ofisinstance. A very notable arraylike instance that does not derive directly from these containers is anumpy.ndarraywhich can be deserialized using the JSON-likemsgpacklibrary withmsgpack_numpy.This changeset aims to add support for arraylike (
list,tupleandnumpy.ndarray) containers in place of parsed JSON arrays and without adding any dependency on thenumpylibrary. This is done using the documented numpy array interface protocol of which many more arraylike libraries adhere to such asxarray,dask,astropyandcupy.(
pandas.Seriesis also arraylike but limited to 1D as multidimensional Series isn't an intended use case and has slicing issues)