-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: non-iterable value in meta raise error in json_normalize #31524
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
Changes from 32 commits
7e461a1
1314059
8bcb313
24c3ede
dea38f2
cd9e7ac
e5e912b
2d21d1e
fcb4b80
8ec4450
a33d05c
6bedc52
1f0f3bc
ce81951
5de348c
3c38c48
130d71b
3ef920f
0b46239
f25a4f4
6eee937
d4d9218
a23eb2d
4c5d61b
9726014
67a43fe
392e3d1
011dbb0
3e74a3a
9476af7
c399983
6165467
7a20b8c
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,11 +3,12 @@ | |||||
|
||||||
from collections import defaultdict | ||||||
import copy | ||||||
from typing import Any, DefaultDict, Dict, Iterable, List, Optional, Union | ||||||
from typing import DefaultDict, Dict, Iterable, List, Optional, Union | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from pandas._libs.writers import convert_json_to_lines | ||||||
from pandas._typing import Scalar | ||||||
from pandas.util._decorators import deprecate | ||||||
|
||||||
import pandas as pd | ||||||
|
@@ -226,14 +227,26 @@ def _json_normalize( | |||||
Returns normalized data with columns prefixed with the given string. | ||||||
""" | ||||||
|
||||||
def _pull_field(js: Dict[str, Any], spec: Union[List, str]) -> Iterable: | ||||||
def _pull_field(js: Dict[str, Scalar], spec: Union[List, str]) -> Scalar: | ||||||
"""Internal function to pull field""" | ||||||
result = js # type: ignore | ||||||
if isinstance(spec, list): | ||||||
for field in spec: | ||||||
result = result[field] | ||||||
result = result[field] # type: ignore | ||||||
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. Can you advise specifically what the error is? I realize we want to get this in for 1.0.2 so not going to block, but I still think this code is suspect (not from your change per se - just a historical artifact) so I'd hate to suppress a warning about another bug that this fix could be introducing 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. Took a look at this locally; I think if you revert some of the other changes here you won't need the ignore. Suggested separately 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. yeah, the error is:
as said, this is because 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.
Suggested change
|
||||||
else: | ||||||
result = result[spec] | ||||||
result = result[spec] # type: ignore | ||||||
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.
Suggested change
|
||||||
return result | ||||||
|
||||||
def _pull_records(js: Dict[str, Scalar], spec: Union[List, str]) -> Iterable: | ||||||
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.
Suggested change
|
||||||
""" | ||||||
Interal function to pull field for records, and similar to | ||||||
_pull_field, but require to return Iterable. And will raise error | ||||||
if has non iterable value. | ||||||
""" | ||||||
result = _pull_field(js, spec) | ||||||
|
||||||
# GH 31507 GH 30145, if result is not Iterable, raise TypeError if not | ||||||
# null, otherwise return an empty list | ||||||
if not isinstance(result, Iterable): | ||||||
if pd.isnull(result): | ||||||
result = [] # type: ignore | ||||||
|
@@ -242,7 +255,6 @@ def _pull_field(js: Dict[str, Any], spec: Union[List, str]) -> Iterable: | |||||
f"{js} has non iterable value {result} for path {spec}. " | ||||||
"Must be iterable or null." | ||||||
) | ||||||
|
||||||
return result | ||||||
|
||||||
if isinstance(data, list) and not data: | ||||||
|
@@ -292,7 +304,7 @@ def _recursive_extract(data, path, seen_meta, level=0): | |||||
_recursive_extract(obj[path[0]], path[1:], seen_meta, level=level + 1) | ||||||
else: | ||||||
for obj in data: | ||||||
recs = _pull_field(obj, path[0]) | ||||||
recs = _pull_records(obj, path[0]) | ||||||
recs = [ | ||||||
nested_to_record(r, sep=sep, max_level=max_level) | ||||||
if isinstance(r, dict) | ||||||
|
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.