-
-
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 raises boardcasting error with list-like metadata #47708
Changes from all commits
039a659
1645550
6db3e6c
49f2950
b73d0f1
8eb1421
de21683
5a3d8bd
a4ac5ec
cc00c3e
7efe709
9ad3db4
23c090e
b3d500f
2779bc2
6252821
34a3ace
f2fea4e
06d9f34
35a8494
8135fea
fccfbba
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 |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
) | ||
from pandas.util._decorators import deprecate | ||
|
||
from pandas.core.dtypes.common import is_list_like | ||
|
||
import pandas as pd | ||
from pandas import DataFrame | ||
|
||
|
@@ -531,7 +533,14 @@ def _recursive_extract(data, path, seen_meta, level=0): | |
raise ValueError( | ||
f"Conflicting metadata name {k}, need distinguishing prefix " | ||
) | ||
result[k] = np.array(v, dtype=object).repeat(lengths) | ||
if v and is_list_like(v[0]): | ||
out = [] | ||
for item, repeat in zip(v, lengths): | ||
for _ in range(repeat): | ||
out.append(item) | ||
else: | ||
out = np.array(v, dtype=object).repeat(lengths).tolist() | ||
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. Here 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. Is the 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. Not necessary, but auto-type-infering won't be triggered if drop this. I think type infer is useful here. If we still want keep the object type, I can change. Suggestion? 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 was hoping this line could avoid 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. How about the following solution? We only point out the object dtype when this is a string np.array and do the if v and isinstance(v[0], str):
out = np.array(v, dtype=object)
else:
out = np.array(v)
out = out.repeat(lengths, axis=0)
if v and is_list_like(v[0]):
out = out.tolist() 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. Actually looking back at the original line, it appears Would it make sense just to combine both branches then and always return a list (without using |
||
result[k] = out | ||
return result | ||
|
||
|
||
|
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.
As an alternative can we not just do something like:
Less than ideal but I think still gets us to the same place? The branching now is a little tough to follow
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.
@mroeschke suggested not to construct the np array for nested data, what I wrote before is
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 think @WillAyd's suggestion is good if it works. My main issue before was one path calling
tolist()
and the other returning annp.ndarray
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.
Late response. Unfortunately this won't work. When v is a
list
,arr
will be[list(...)]
, which raises boardcasting error. Classified discussions may still be necessary. (Now both paths returnlist
for consistency and auto-type-infering.) @mroeschke