Add support for 'arraylike' objects as JSON arrays#317
Add support for 'arraylike' objects as JSON arrays#317calgray wants to merge 7 commits intojmespath:developfrom
Conversation
Co-authored-by: Dobatymo <Dobatymo@users.noreply.github.com>
jmespath/functions.py
Outdated
|
|
||
|
|
||
| def is_array(arg): | ||
| return hasattr(arg, "__array__") and arg.shape != () |
There was a problem hiding this comment.
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.
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).
There was a problem hiding this comment.
Oh, I see what you mean. Updated to:
return hasattr(arg, "__array__") and arg.__array__.shape != ()
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)