Skip to content
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

Closed
wants to merge 22 commits into from

Conversation

GYHHAHA
Copy link
Contributor

@GYHHAHA GYHHAHA commented Jul 13, 2022

@mroeschke mroeschke added this to the 1.5 milestone Jul 13, 2022
@mroeschke mroeschke added the IO JSON read_json, to_json, json_normalize label Jul 13, 2022
@GYHHAHA GYHHAHA requested a review from mroeschke July 25, 2022 04:05
pandas/io/json/_normalize.py Outdated Show resolved Hide resolved
GYHHAHA and others added 2 commits July 30, 2022 12:13
fix list like

Co-authored-by: Matthew Roeschke <[email protected]>
@pep8speaks
Copy link

pep8speaks commented Jul 30, 2022

Hello @GYHHAHA! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2022-08-18 02:41:26 UTC

@@ -531,7 +532,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]):
Copy link
Member

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:

arr = np.empty(1, dtype=object)
arr[0] = v
arr = arr.repeat(lengths)

Less than ideal but I think still gets us to the same place? The branching now is a little tough to follow

Copy link
Contributor Author

@GYHHAHA GYHHAHA Aug 6, 2022

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

result[k] = np.array(v, dtype=object).repeat(lengths, axis=0).tolist()

Copy link
Member

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 an np.ndarray

Copy link
Contributor Author

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 return list for consistency and auto-type-infering.) @mroeschke

for _ in range(repeat):
out.append(item)
else:
out = np.array(v, dtype=object).repeat(lengths).tolist()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here dtype=object is necessary since np.array(["a", np.nan]) will result in the missing value is converted to string format "nan".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the tolist call needed as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping this line could avoid dtype=object and tolist such that this line doesn't trigger type inference and would be more performant. Otherwise since both if/else blocks convert to list are essentially similar.

Copy link
Contributor Author

@GYHHAHA GYHHAHA Aug 18, 2022

Choose a reason for hiding this comment

The 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 tolist() when nested array detected. For the numeric dtype, we can still keep performant and handle both two corner cases.

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()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually looking back at the original line, it appears dtype=object was originally here and allowing dtype inference all the time might actually lead to more desired behavior (more specific types).

Would it make sense just to combine both branches then and always return a list (without using np.array)? Would be good to also to explore if there's any performance hit

@GYHHAHA GYHHAHA requested a review from mroeschke August 6, 2022 23:49
@mroeschke mroeschke removed this from the 1.5 milestone Aug 22, 2022
@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Sep 22, 2022
@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO JSON read_json, to_json, json_normalize Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: json_normalize fails with empty arrays/lists BUG: json_normalize cannot parse metadata fields list type
5 participants