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
Closed
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ I/O
- Bug in :func:`read_sas` with RLE-compressed SAS7BDAT files that contain 0x00 control bytes (:issue:`47099`)
- Bug in :func:`read_parquet` with ``use_nullable_dtypes=True`` where ``float64`` dtype was returned instead of nullable ``Float64`` dtype (:issue:`45694`)
- Bug in :meth:`DataFrame.to_json` where ``PeriodDtype`` would not make the serialization roundtrip when read back with :meth:`read_json` (:issue:`44720`)
- Bug in :func:`json_normalize` raised boardcasting error with list-like metadata (:issue:`37782`, :issue:`47182`)

Period
^^^^^^
Expand Down
9 changes: 8 additions & 1 deletion pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,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 isinstance(v[0], abc.Iterable):
GYHHAHA marked this conversation as resolved.
Show resolved Hide resolved
out = []
for item, repeat in zip(v, lengths):
for _ in range(repeat):
out.append(item)
else:
out = np.array(v).repeat(lengths)
GYHHAHA marked this conversation as resolved.
Show resolved Hide resolved
result[k] = out
return result


Expand Down
33 changes: 31 additions & 2 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_meta_non_iterable(self):

result = json_normalize(json.loads(data), record_path=["data"], meta=["id"])
expected = DataFrame(
{"one": [1], "two": [2], "id": np.array([99], dtype=object)}
{"one": [1], "two": [2], "id": np.array([99], dtype="int64")}
)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -641,7 +641,7 @@ def test_missing_nested_meta(self):
ex_data = [[1, "foo", np.nan], [2, "foo", np.nan]]
columns = ["rec", "meta", "nested_meta.leaf"]
expected = DataFrame(ex_data, columns=columns).astype(
{"nested_meta.leaf": object}
{"nested_meta.leaf": "float"}
)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -891,3 +891,32 @@ def test_series_non_zero_index(self):
}
)
tm.assert_frame_equal(result, expected)

def test_list_type_meta_data(self):
# GH 37782
data = {"values": [1, 2, 3], "metadata": {"listdata": [1, 2]}}
result = json_normalize(
data=data,
record_path=["values"],
meta=[["metadata", "listdata"]],
)
expected = DataFrame(
{
0: [1, 2, 3],
"metadata.listdata": [[1, 2], [1, 2], [1, 2]],
}
)
tm.assert_frame_equal(result, expected)

def test_empty_list_data(self):
# GH 47182
data = [
{"id": 1, "path": [{"a": 3, "b": 4}], "emptyList": []},
{"id": 2, "path": [{"a": 5, "b": 6}], "emptyList": []},
]
result = json_normalize(data, "path", ["id", "emptyList"])
expected = DataFrame(
[[3, 4, 1, []], [5, 6, 2, []]],
columns=["a", "b", "id", "emptyList"],
)
tm.assert_frame_equal(result, expected)