-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Support Infinity, -Infinity and NaN in read_json #12213
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
Comments
nan's are already supported.
|
But not |
that's not valid json |
I know, that's what I wrote in the first post. But it is a commonly used extension. See the description in Google's GSON package:
Or python's json module:
|
Also interesting: the json specifications explicitly allow to accept extensions of the standard:
|
👍 This would indeed be very helpful for interoperability between a pandas backend and a JS frontend. |
I took a crack at this and discovered that the json library underlying read_json, ujson, doesn't handle NaN nor infinity. In previous discussions (more) the authors of ujson resisted adding support because it's out of spec, and there's no way to specify a custom decodor nor encoder with ujson. |
The newest master throws a Script to reproduce (using Hypothesis): import json
from hypothesis import assume, example, given
import hypothesis.strategies as st
import pandas as pd
FAILING_EXAMPLES = (
[{u'': float('inf')}],
[{u'': float('nan')}],
)
@given(st.lists(st.dictionaries(st.text(),
st.floats() | st.booleans() | st.text() | st.none())))
@example([{u'': float('inf')}])
@example([{u'': float('nan')}])
def test_load_json(test_input):
df = pd.read_json(json.dumps(test_input)) |
Hi there, Thanks for the thorough discussions. We would suppose that pandas to be friendly to data scientists and a little surprised to find compatibility issues with IEEE float standards. Infinity/-Infinity is particularly useful because it is flexible enough to be clipped to the max/min value in any valid ranges. This is not possible with NaN/null. As of today, it is possible to encode Infinity using Thanks. |
pandas choosing not to be pragmatic and not support them, so I choose to use the standard library json module which does pandas-dev/pandas#12213
Finally! Thanks to anyone involved in making this possible. |
While these special values are not strictly standard conform, most implementations do allow or use them.
For example Google's GSON Library or python's json module in the standard library.
The text was updated successfully, but these errors were encountered: