Skip to content

BUG: handle none values in json.normalize() #53773

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
meta_keys = [sep.join(val) for val in _meta]

def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
if data is None:
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this generally an issue if data is not list like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While running the tests I noticed while passing scalar values must be done in a list as is the expected behavior. But if I have a row of data in my json and one of the field values is null then the normalize() fails by throwing an exception when I instead expect it to ignore the entire row, which this change should do.

Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test where a nested json value is a scalar like 2 as assert the result?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like this as my expected value?

expected = DataFrame(
            {
                "nested_field_id": 1,
            }
        )

Choose a reason for hiding this comment

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

hi all, what is the status on this one? Any idea when it could be merged? (original request: #53719)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mroeschke Hi. In regard to the previous comment by me, is that what is required in the tests?

Copy link
Member

Choose a reason for hiding this comment

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

A test where the input has something like "nested_field": [{"nested_field_id": [1]}],

return
if isinstance(data, dict):
data = [data]
if len(path) > 1:
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,29 @@ def test_top_column_with_leading_underscore(self):

tm.assert_frame_equal(result, expected)

def test_column_with_none(self):
# 53719
data = {
"root": [
{
"id": 1,
"nested_field": [{"nested_field_id": 1}],
},
{"id": 2, "nested_field": None},
]
}
result = json_normalize(
data,
record_path=["root", "nested_field"],
)
expected = DataFrame(
{
"nested_field_id": [1],
}
)

tm.assert_frame_equal(result, expected)


class TestNestedToRecord:
def test_flat_stays_flat(self):
Expand Down