Skip to content

Commit cf0a55f

Browse files
vuminhlejorisvandenbossche
authored andcommitted
BUG: Fix json_normalize throwing TypeError (#21536) (#21540)
(cherry picked from commit 5fdaa97)
1 parent 2d2f6aa commit cf0a55f

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v0.23.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Bug Fixes
6565
**I/O**
6666

6767
- Bug in :func:`read_csv` that caused it to incorrectly raise an error when ``nrows=0``, ``low_memory=True``, and ``index_col`` was not ``None`` (:issue:`21141`)
68-
-
68+
- Bug in :func:`json_normalize` when formatting the ``record_prefix`` with integer columns (:issue:`21536`)
6969
-
7070

7171
**Plotting**

pandas/io/json/normalize.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ def json_normalize(data, record_path=None, meta=None,
170170
3 Summit 1234 John Kasich Ohio OH
171171
4 Cuyahoga 1337 John Kasich Ohio OH
172172
173+
>>> data = {'A': [1, 2]}
174+
>>> json_normalize(data, 'A', record_prefix='Prefix.')
175+
Prefix.0
176+
0 1
177+
1 2
173178
"""
174179
def _pull_field(js, spec):
175180
result = js
@@ -259,7 +264,8 @@ def _recursive_extract(data, path, seen_meta, level=0):
259264
result = DataFrame(records)
260265

261266
if record_prefix is not None:
262-
result.rename(columns=lambda x: record_prefix + x, inplace=True)
267+
result = result.rename(
268+
columns=lambda x: "{p}{c}".format(p=record_prefix, c=x))
263269

264270
# Data types, a problem
265271
for k, v in compat.iteritems(meta_vals):

pandas/tests/io/json/test_normalize.py

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def test_simple_normalize_with_separator(self, deep_nested):
123123
'country', 'states_name']).sort_values()
124124
assert result.columns.sort_values().equals(expected)
125125

126+
def test_value_array_record_prefix(self):
127+
# GH 21536
128+
result = json_normalize({'A': [1, 2]}, 'A', record_prefix='Prefix.')
129+
expected = DataFrame([[1], [2]], columns=['Prefix.0'])
130+
tm.assert_frame_equal(result, expected)
131+
126132
def test_more_deeply_nested(self, deep_nested):
127133

128134
result = json_normalize(deep_nested, ['states', 'cities'],

0 commit comments

Comments
 (0)