Skip to content

BUG: fix nested meta path bug (GH 27220) #27667

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
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ def _recursive_extract(data, path, seen_meta, level=0):
if len(path) > 1:
for obj in data:
for val, key in zip(meta, meta_keys):
if level + 1 == len(val):
seen_meta[key] = _pull_field(obj, val[-1])
# if level + 1 == len(val):
seen_meta[key] = _pull_field(obj, val[0])

_recursive_extract(obj[path[0]], path[1:], seen_meta, level=level + 1)
else:
for obj in data:
for ind, obj in enumerate(data):
recs = _pull_field(obj, path[0])
recs = [
nested_to_record(r, sep=sep, max_level=max_level)
Expand All @@ -305,8 +305,16 @@ def _recursive_extract(data, path, seen_meta, level=0):
# For repeating the metadata later
lengths.append(len(recs))
for val, key in zip(meta, meta_keys):

if level + 1 > len(val):
meta_val = seen_meta[key]
meta_vals[key].append(meta_val)
elif seen_meta:
meta_val = seen_meta[key]
if isinstance(meta_val, list):
meta_vals[key].append(meta_val[ind][val[level]])
else:
meta_vals[key].append(meta_val[val[level]])
else:
try:
meta_val = _pull_field(obj, val[level:])
Expand All @@ -319,7 +327,8 @@ def _recursive_extract(data, path, seen_meta, level=0):
"errors='ignore' as key "
"{err} is not always present".format(err=e)
)
meta_vals[key].append(meta_val)
meta_vals[key].append(meta_val)

records.extend(recs)

_recursive_extract(data, record_path, {}, level=0)
Expand Down
80 changes: 69 additions & 11 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ def test_empty_array(self):

def test_simple_normalize_with_separator(self, deep_nested):
# GH 14883
result = json_normalize({"A": {"A": 1, "B": 2}})
expected = DataFrame([[1, 2]], columns=["A.A", "A.B"])
tm.assert_frame_equal(result.reindex_like(expected), expected)

result = json_normalize({"A": {"A": 1, "B": 2}}, sep="_")
expected = DataFrame([[1, 2]], columns=["A_A", "A_B"])
tm.assert_frame_equal(result.reindex_like(expected), expected)

result = json_normalize({"A": {"A": 1, "B": 2}}, sep="\u03c3")
expected = DataFrame([[1, 2]], columns=["A\u03c3A", "A\u03c3B"])
tm.assert_frame_equal(result.reindex_like(expected), expected)
# result = json_normalize({"A": {"A": 1, "B": 2}})
# expected = DataFrame([[1, 2]], columns=["A.A", "A.B"])
# tm.assert_frame_equal(result.reindex_like(expected), expected)
#
# result = json_normalize({"A": {"A": 1, "B": 2}}, sep="_")
# expected = DataFrame([[1, 2]], columns=["A_A", "A_B"])
# tm.assert_frame_equal(result.reindex_like(expected), expected)
#
# result = json_normalize({"A": {"A": 1, "B": 2}}, sep="\u03c3")
# expected = DataFrame([[1, 2]], columns=["A\u03c3A", "A\u03c3B"])
# tm.assert_frame_equal(result.reindex_like(expected), expected)

result = json_normalize(
deep_nested,
Expand Down Expand Up @@ -287,6 +287,64 @@ def test_shallow_nested(self):
expected = DataFrame(ex_data, columns=result.columns)
tm.assert_frame_equal(result, expected)

def test_nested_meta_path_with_nested_record_path(self, state_data):
# GH 27220
result = json_normalize(
state_data,
["counties", "name"],
["state", "shortname", ["info", "governor"]],
errors="ignore",
)
ex_data = {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using a dict can you construct this with literal values? If you use a list of lists can circumvent 3.5 ordering issues with a dict

If you can simplify expectation would help a lot as well so as not to write out the same values 21 times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. It is indeed better. I simply copied other test cases. I will change accordingly.

0: [
"D",
"a",
"d",
"e",
"B",
"r",
"o",
"w",
"a",
"r",
"d",
"P",
"a",
"l",
"m",
" ",
"B",
"e",
"a",
"c",
"h",
"S",
"u",
"m",
"m",
"i",
"t",
"C",
"u",
"y",
"a",
"h",
"o",
"g",
"a",
],
"state": ["Florida"] * 21 + ["Ohio"] * 14,
"shortname": ["FL"] * 21 + ["OH"] * 14,
"info.governor": ["Rick Scott"] * 21 + ["John Kasich"] * 14,
"population": [12345] * 4
+ [40000] * 7
+ [60000] * 10
+ [1234] * 6
+ [1337] * 8,
}
expected = DataFrame(ex_data, columns=result.columns)
tm.assert_frame_equal(result, expected)

def test_meta_name_conflict(self):
data = [
{
Expand Down