-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: json_normalize cannot parse metadata fields list type #37782
Comments
Thanks @sann05 for the report. a possible fix ... diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py
index e77d60d2d4..d04d321388 100644
--- a/pandas/io/json/_normalize.py
+++ b/pandas/io/json/_normalize.py
@@ -531,7 +531,16 @@ def _json_normalize(
raise ValueError(
f"Conflicting metadata name {k}, need distinguishing prefix "
)
- result[k] = np.array(v, dtype=object).repeat(lengths)
+
+ values = np.array(v, dtype=object)
+
+ if values.ndim > 1:
+ # GH#37782
+ values = np.empty((len(v),), dtype=object)
+ for i, v in enumerate(v):
+ values[i] = v
+
+ result[k] = values.repeat(lengths)
return result
there maybe a more elegant solution of similar code elsewhere in the codebase that could be reused. PRs to fix welcome. |
I have similar problem .. https://stackoverflow.com/questions/72801399/2d-arrays-repeat-throwing-valueerror-operands-could-not-be-broadcast-together Any plan to fix this issue? |
I have the same problem as described here. In addition to the problem that lists with zero or more than one value return a ValueError I think it is important to mention that lists with 1 element must not be converted to a scalars in any case, the list must remain! |
That's a fix for me. |
* Fix BUG: #37782 * Fix BUG: #37782 * Fix BUG: 37782 - Hardcoded test data * Fix BUG: 37782 - Hardcoded test data * Update pandas/io/json/_normalize.py Co-authored-by: Matthew Roeschke <[email protected]> * Fix BUG: 37782 - typo * Fix BUG: 37782 - typo * Update doc/source/whatsnew/v2.1.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
* Fix BUG: pandas-dev#37782 * Fix BUG: pandas-dev#37782 * Fix BUG: 37782 - Hardcoded test data * Fix BUG: 37782 - Hardcoded test data * Update pandas/io/json/_normalize.py Co-authored-by: Matthew Roeschke <[email protected]> * Fix BUG: 37782 - typo * Fix BUG: 37782 - typo * Update doc/source/whatsnew/v2.1.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
* Fix BUG: pandas-dev#37782 * Fix BUG: pandas-dev#37782 * Fix BUG: 37782 - Hardcoded test data * Fix BUG: 37782 - Hardcoded test data * Update pandas/io/json/_normalize.py Co-authored-by: Matthew Roeschke <[email protected]> * Fix BUG: 37782 - typo * Fix BUG: 37782 - typo * Update doc/source/whatsnew/v2.1.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
Code Sample, a copy-pastable example
Problem description
It throws error
ValueError: Length of values (6) does not match length of index (3)
Changing listdata field from
[1,2]
to{1,2}
helped but I can't control JSON I receive.Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 67a3d42
python : 3.6.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-52-generic
Version : #57-Ubuntu SMP Thu Oct 15 10:57:00 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.4
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
The text was updated successfully, but these errors were encountered: