can't have numpy datatypes in attributes
See original GitHub issueWe are working on the zarr backend for XArray (pydata/xarray#1528). XArray likes to put all kinds of weird stuff into attributes, including numpy datatypes and even numpy arrays. This is because the netCDF data model allows attributes to have all of the same types as variables.
Instead, in zarr, the attributes have to be json-serializable. So this doesn’t work:
za = zarr.create(shape=(1), store='tmp_file')
za.attrs['foo'] = np.float32(0)
It raises TypeError: Object of type 'float32' is not JSON serializable
.
We will need some sort of workaround for this in order to make zarr work as a store for xarray.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
can't have numpy datatypes in attributes · Issue #156 - GitHub
This is because the netCDF data model allows attributes to have all of the same types as variables. ... It raises TypeError: Object...
Read more >Data type objects (dtype) — NumPy v1.24 Manual
Structured data types are formed by creating a data type whose field contain other data types. Each field has a name by which...
Read more >module 'numpy' has no attribute 'dtype' - python - Stack Overflow
I figured this out. The answer is that the file I was running was named numbers.py . This screws the whole thing up....
Read more >NumPy Data Types - W3Schools
NumPy has some extra data types, and refer to data types with one ... A non integer string like 'a' can not be...
Read more >NumPy - Data Types - Tutorialspoint
NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I would like to store as attributes any of the data types described in the “Data Type Encoding” section of the Zarr specification.
Specifically, in my real-world usage, I have encountered inconvenience with attribute values that are
datetime64
andtimedelta
f8
versusf4
), which JSON doesn’t distinguishNaN
, which has an exact representation as a Zarr/NumPy floating-point value but can not be represented by JSON[('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')]
I am also excited by the possibility of storing attributes that are arbitrary objects, such as JSON documents, although I haven’t expressly encountered this requirement yet.
It is worth noting that, in NetCDF, attribute values are really 1-dimensional arrays:
I was recently hit by this very same problem, with reference to HDF5 files, which also allow for array attributes.
For example from
h5dump
I havewhich are rendered by
h5py
asWhen converting from HDF5 to ZARR,
zarr.copy_all
fails withSince I have a bunch of files to convert I implemented a quick fix in miccoli/zarr-python@380ee7c07
I’m not sure if this is of general interest, but if there is enough interest I can open a PR.
Open question:
np.ndarray -> list
mapping, or maybe better, allow the user to override the default JSONEncoder?See also #933 and #533