Skip to content

Commit af463df

Browse files
committed
BUG: Fix TypeError in json_normalize with non-str meta key and record_path
1 parent 00a7c41 commit af463df

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

pandas/io/json/_normalize.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,12 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
552552
lengths = []
553553

554554
meta_vals: DefaultDict = defaultdict(list)
555-
meta_keys = [sep.join(val) for val in _meta]
555+
meta_keys = []
556+
for val in _meta:
557+
if len(val) == 1:
558+
meta_keys.append(val[0])
559+
else:
560+
meta_keys.append(sep.join(str(x) for x in val))
556561

557562
def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
558563
if isinstance(data, dict):
@@ -568,9 +573,11 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
568573
for obj in data:
569574
recs = _pull_records(obj, path[0])
570575
recs = [
571-
nested_to_record(r, sep=sep, max_level=max_level)
572-
if isinstance(r, dict)
573-
else r
576+
(
577+
nested_to_record(r, sep=sep, max_level=max_level)
578+
if isinstance(r, dict)
579+
else r
580+
)
574581
for r in recs
575582
]
576583

pandas/tests/io/json/test_normalize.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,34 @@ def test_series_index(self, state_data):
593593
result = json_normalize(series, "counties")
594594
tm.assert_index_equal(result.index, idx.repeat([3, 2]))
595595

596+
def test_json_normalize_int_key_with_record_path(self):
597+
# 63019
598+
data = [
599+
{
600+
"a": 1,
601+
12: "meta_value_1",
602+
"nested": [{"b": 2, "c": 3}],
603+
},
604+
{
605+
"a": 6,
606+
12: "meta_value_2",
607+
"nested": [{"b": 7, "c": 8}],
608+
},
609+
]
610+
611+
result = json_normalize(data, record_path=["nested"], meta=[12, "a"])
612+
613+
expected_data = {
614+
"b": [2, 7],
615+
"c": [3, 8],
616+
12: ["meta_value_1", "meta_value_2"],
617+
"a": [1, 6],
618+
}
619+
expected_columns = ["b", "c", 12, "a"]
620+
expected = DataFrame(expected_data, columns=expected_columns)
621+
622+
tm.assert_frame_equal(result, expected)
623+
596624

597625
class TestNestedToRecord:
598626
def test_flat_stays_flat(self):

0 commit comments

Comments
 (0)