Skip to content

Commit 1c186aa

Browse files
committed
BUG: fix nested meta path bug in json_normalize (GH 27220)
1 parent 9c568b6 commit 1c186aa

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

pandas/io/json/_normalize.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,16 @@ def _recursive_extract(data, path, seen_meta, level=0):
310310
# is at the meta path end
311311
if level + 1 > len(val):
312312
meta_val = seen_meta[key]
313-
meta_vals[key].append(meta_val)
314313
# Extract the value of the key from seen_meta when
315314
# meta path and record path are on two branches
316315
elif seen_meta:
317-
meta_val = seen_meta[key]
318-
meta_vals[key] += [
319-
# The list case
320-
meta_val[ind][val[level]]
321-
if isinstance(meta_val, list)
322-
# The dict case
323-
else meta_val[val[level]]
324-
]
316+
meta_val_obj = seen_meta[key]
317+
# Both the list case and the dict case are covered
318+
meta_val = (
319+
meta_val_obj[ind][val[level]]
320+
if isinstance(meta_val_obj, list)
321+
else meta_val_obj[val[level]]
322+
)
325323
# At top level, seen_meta is empty, pull from data
326324
# directly and raise KeyError if not found
327325
else:
@@ -336,7 +334,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
336334
"errors='ignore' as key "
337335
"{err} is not always present".format(err=e)
338336
)
339-
meta_vals[key].append(meta_val)
337+
meta_vals[key].append(meta_val)
340338

341339
records.extend(recs)
342340

pandas/tests/io/json/test_normalize.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import sys
32

43
import numpy as np
54
import pytest
@@ -288,13 +287,13 @@ def test_shallow_nested(self):
288287
expected = DataFrame(ex_data, columns=result.columns)
289288
tm.assert_frame_equal(result, expected)
290289

291-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="drop support for 3.5 soon")
290+
@pytest.mark.skipif(not PY36, reason="drop support for 3.5 soon")
292291
def test_nested_meta_path_with_nested_record_path(self, state_data):
293292
# GH 27220
294293
result = json_normalize(
295-
state_data,
296-
["counties", "name"],
297-
["state", "shortname", ["info", "governor"]],
294+
data=state_data,
295+
record_path=["counties", "name"],
296+
meta=["state", "shortname", ["info", "governor"]],
298297
errors="ignore",
299298
)
300299
ex_data = {

0 commit comments

Comments
 (0)