From b4337e26ab80b57768c71e6bbb13feb32946de44 Mon Sep 17 00:00:00 2001 From: Maximilian Nicholson Date: Thu, 11 Sep 2025 18:34:04 +0100 Subject: [PATCH 1/2] fix to #62205 --- pandas/io/json/_normalize.py | 5 ++++- pandas/tests/io/json/test_normalize.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py index 642408b35ba24..71a0c674eebac 100644 --- a/pandas/io/json/_normalize.py +++ b/pandas/io/json/_normalize.py @@ -524,7 +524,10 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list: # TODO: handle record value which are lists, at least error # reasonably data = nested_to_record(data, sep=sep, max_level=max_level) - return DataFrame(data, index=index) + result = DataFrame(data, index=index) + if record_prefix is not None: + result = result.rename(columns=lambda x: f"{record_prefix}{x}") + return result elif not isinstance(record_path, list): record_path = [record_path] diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index cde0a7a378cff..2d14bbc006816 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -380,6 +380,15 @@ def test_record_prefix(self, state_data): tm.assert_frame_equal(result, expected) + def test_record_prefix_no_record_path_series(self): + # Ensure record_prefix is applied when record_path is None for Series input + s = Series([{"k": f"{i}", "m": "q"} for i in range(3)]) + result = json_normalize(s, record_prefix="T.") + expected = DataFrame( + [{"T.k": "0", "T.m": "q"}, {"T.k": "1", "T.m": "q"}, {"T.k": "2", "T.m": "q"}] + ) + tm.assert_frame_equal(result, expected) + def test_non_ascii_key(self): testjson = ( b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},' From e733b8a6fd90ce97f49f44dc41c6bc79fbe3609c Mon Sep 17 00:00:00 2001 From: Maximilian Nicholson Date: Thu, 11 Sep 2025 18:50:09 +0100 Subject: [PATCH 2/2] reduced length of characters on a line for pre-checks. --- pandas/tests/io/json/test_normalize.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index 2d14bbc006816..b6212b514673f 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -385,7 +385,11 @@ def test_record_prefix_no_record_path_series(self): s = Series([{"k": f"{i}", "m": "q"} for i in range(3)]) result = json_normalize(s, record_prefix="T.") expected = DataFrame( - [{"T.k": "0", "T.m": "q"}, {"T.k": "1", "T.m": "q"}, {"T.k": "2", "T.m": "q"}] + [ + {"T.k": "0", "T.m": "q"}, + {"T.k": "1", "T.m": "q"}, + {"T.k": "2", "T.m": "q"}, + ] ) tm.assert_frame_equal(result, expected)