Skip to content

Commit d134cfe

Browse files
committed
BUG: Fix record_prefix ignored when record_path is empty (GH 62205)
- Modified early return optimization to exclude cases where record_prefix is provided - Added proper prefix application using nested_to_record for consistent separator handling - Added comprehensive test case to verify the fix - Maintains backward compatibility and performance optimization for cases without record_prefix Fixes #62205
1 parent 3e1d6d5 commit d134cfe

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/io/json/_normalize.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,16 @@ 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+
528+
result = DataFrame(data, index=index)
529+
530+
# Apply record_prefix if provided, even when record_path is None
531+
if record_prefix is not None:
532+
# Use nested_to_record with prefix to properly handle separators
533+
prefixed_data = nested_to_record(data, prefix=record_prefix, sep=sep, level=1)
534+
result = DataFrame(prefixed_data, index=index)
535+
536+
return result
528537
elif not isinstance(record_path, list):
529538
record_path = [record_path]
530539

pandas/tests/io/json/test_normalize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,16 @@ def test_series_index(self, state_data):
569569
result = json_normalize(series, "counties")
570570
tm.assert_index_equal(result.index, idx.repeat([3, 2]))
571571

572+
def test_record_prefix_without_record_path(self):
573+
# GH 62205: record_prefix should be applied even when record_path is None
574+
data = Series([{"k": f"{i}", "m": "q"} for i in range(5)])
575+
result = json_normalize(data, record_prefix="T")
576+
expected = DataFrame({
577+
"T.k": [f"{i}" for i in range(5)],
578+
"T.m": ["q"] * 5
579+
})
580+
tm.assert_frame_equal(result, expected)
581+
572582

573583
class TestNestedToRecord:
574584
def test_flat_stays_flat(self):

0 commit comments

Comments
 (0)