Skip to content

Commit 88f299d

Browse files
authored
BUG: fix to #62205 (#62320)
1 parent c1b03e7 commit 88f299d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/io/json/_normalize.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,10 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
524524
# TODO: handle record value which are lists, at least error
525525
# reasonably
526526
data = nested_to_record(data, sep=sep, max_level=max_level)
527-
return DataFrame(data, index=index)
527+
result = DataFrame(data, index=index)
528+
if record_prefix is not None:
529+
result = result.rename(columns=lambda x: f"{record_prefix}{x}")
530+
return result
528531
elif not isinstance(record_path, list):
529532
record_path = [record_path]
530533

pandas/tests/io/json/test_normalize.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ def test_record_prefix(self, state_data):
380380

381381
tm.assert_frame_equal(result, expected)
382382

383+
def test_record_prefix_no_record_path_series(self):
384+
# Ensure record_prefix is applied when record_path is None for Series input
385+
s = Series([{"k": f"{i}", "m": "q"} for i in range(3)])
386+
result = json_normalize(s, record_prefix="T.")
387+
expected = DataFrame(
388+
[
389+
{"T.k": "0", "T.m": "q"},
390+
{"T.k": "1", "T.m": "q"},
391+
{"T.k": "2", "T.m": "q"},
392+
]
393+
)
394+
tm.assert_frame_equal(result, expected)
395+
383396
def test_non_ascii_key(self):
384397
testjson = (
385398
b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},'

0 commit comments

Comments
 (0)