-
-
Notifications
You must be signed in to change notification settings - Fork 19.9k
ENH/BUG: Utilize _values_for_json in Series.to_json
#65127
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: main
Are you sure you want to change the base?
Changes from 12 commits
daff749
896933e
df6628f
0cdc017
5146eeb
f80b88e
966f55f
153ed3b
c68a89a
e2a0234
21f6252
e4e9139
c1dff8e
09bed13
49b7429
30a34c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import inspect | ||
| from io import StringIO | ||
| import operator | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from pandas._libs._ujson import ujson_dumps | ||
| from pandas._typing import Dtype | ||
|
|
||
| from pandas.core.dtypes.common import is_bool_dtype | ||
|
|
@@ -783,3 +785,23 @@ def test_equals(self, data, na_value, as_series, box): | |
| def test_equals_same_data_different_object(self, data): | ||
| # https://github.com/pandas-dev/pandas/issues/34660 | ||
| assert pd.Series(data).equals(pd.Series(data)) | ||
|
|
||
| def test_values_for_json(self, data): | ||
| # GH 65047 | ||
| values: np.ndarray = data._values_for_json() | ||
|
|
||
| # Check that the result is a numpy array | ||
| assert type(values) == np.ndarray | ||
|
Julian-Harbeck marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Check that the result is JSON-serializable | ||
| assert isinstance(ujson_dumps(values), str) | ||
|
|
||
| # Check that Series.to_json uses _values_for_json | ||
| ser = pd.Series(data) | ||
| result: str = ser.to_json() | ||
| expected: str = pd.Series(values).to_json() | ||
| assert result == expected | ||
|
|
||
| # Test roundtrip through JSON | ||
| ser_new = pd.read_json(StringIO(result), typ="series", dtype=data.dtype) | ||
| tm.assert_series_equal(ser_new, ser) | ||
|
Comment on lines
+807
to
+811
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe can split this roundtrip part as a separate test, since I think this is the main reason for the xfails? Then the base test ensuring
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes had the same thought earlier (see comment above), I split the test into
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is good to keep the tests together indeed |
||
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.
I suppose categorical or sparse will now be covered by the
_values_for_jsonabove?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.
@jorisvandenbossche As
_values_for_jsonis not overwritten by them they use the base implementation so fall back tonp.asarray(self)which will callself.__array__()so the behaviour should be the same for them as before:pandas/pandas/core/arrays/base.py
Lines 2471 to 2482 in a22ca70
I am not sure if these are the only two arrays that lead to this code path in
objToJSON.c, if so it could be removed as it is replaced by the path above?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.
Yeah, potentially, but at least the comment is no longer correct.
I think there might be some more things that can be cleaned up, but fine to leave that for a follow-up PR as well
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.
Then just remove the comment for now? For testing I just removed this whole block:
And then let the whole test suite run locally (on current dev environment) with some errors failing, but on a first glance nothing that had to do with changes here (mostly SQL, timezone, clipboard). When running
pytest -k jsonthe tests all passed. But I am not sure if that guarantees that nothing got broken, until now JSON serialization of EAs was not tested, so not sure about the coverage of other arrays.