-
Notifications
You must be signed in to change notification settings - Fork 23
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
Roundtripping inconsistent for objects of shape=()
and numpy scalars...
#98
Comments
Nice thanks for all the effort you put into this! Mathematically the distinction between 0d array and scalar seems weird, but I guess from a type system perspective it makes sense. And in any case numpy's eccentricities are not in our control.
The test suite isn't trying to work around the warning, it's testing the function
The easiest way to see this behaviour is to add But it seems good to support 0dimensional arrays, and with a few adjustments I think it'll work great. Thanks again! |
I'm really not sure how to reconcile the fact that the default The issue seems to stem from the fact that numpy's import numpy as np
np.float64.mro()
[numpy.float64,
numpy.floating,
numpy.inexact,
numpy.number,
numpy.generic,
float,
object] Eventually the We can
|
I implemented this in this commit 70bacca it copies alot of code from cpython at the very least we should split this off into a file to keep correct attribution to their license. i change 3 lines that had |
It seems that there are some edge cases with serializing scalars and numpy arrays with 0-dimensions
This has always confused me, so for precision, I quote numpy's documentation
So I added the following test to the file
tests/test_np.py
to see if things serialize correctly:After round tripping, we aren't preserving the "0-dimension" and it is being downcast to a numpy-scalar.
This strange behavior leads to the test suite using
encode_scalars_inplace
as an attempt to workaround the warning that "scalars cannot be reliably encoded", then turns around to using the strange "automatic downcast" behavior to recover the original structure.https://github.com/mverleg/pyjson_tricks/blob/master/tests/test_np.py#L170
However, this would be incorrect if the user mixes "0-dimensional" numpy arrays.
Now, I originally came here to try to address a pretty specific usecase of ours: I am trying to serialize numpy datetime64 scalars. I tried to augment them to 0-dimensional arrays, however, this breaks two assumptions made in pyjson_tricks:
numpy.datetime64
objects are notnumpy.generics
used in the encodernumpy.datetime64
constructors cannot be obtained with thegetattr
used in the decoder since they have units (annoying I know, date is complicated)My proposal unfortunately requires breaking anybody using
replace_scalars_inplace
such as the test suiteOther references
The text was updated successfully, but these errors were encountered: