Skip to content

Commit 17c90ea

Browse files
vuminhlePingviinituutti
authored andcommitted
BUG: Fix json_normalize throwing TypeError when record_path has a sequence of dicts pandas-dev#22706 (pandas-dev#22804)
1 parent d7a3fdf commit 17c90ea

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
15111511
- Bug in :meth:`DataFrame.to_dict` when the resulting dict contains non-Python scalars in the case of numeric data (:issue:`23753`)
15121512
- :func:`DataFrame.to_string()`, :func:`DataFrame.to_html()`, :func:`DataFrame.to_latex()` will correctly format output when a string is passed as the ``float_format`` argument (:issue:`21625`, :issue:`22270`)
15131513
- Bug in :func:`read_csv` that caused it to raise ``OverflowError`` when trying to use 'inf' as ``na_value`` with integer index column (:issue:`17128`)
1514+
- Bug in :func:`json_normalize` that caused it to raise ``TypeError`` when two consecutive elements of ``record_path`` are dicts (:issue:`22706`)
15141515

15151516
Plotting
15161517
^^^^^^^^

pandas/io/json/normalize.py

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def _pull_field(js, spec):
229229
meta_keys = [sep.join(val) for val in meta]
230230

231231
def _recursive_extract(data, path, seen_meta, level=0):
232+
if isinstance(data, dict):
233+
data = [data]
232234
if len(path) > 1:
233235
for obj in data:
234236
for val, key in zip(meta, meta_keys):

pandas/tests/io/json/test_normalize.py

+15
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ def test_value_array_record_prefix(self):
129129
expected = DataFrame([[1], [2]], columns=['Prefix.0'])
130130
tm.assert_frame_equal(result, expected)
131131

132+
def test_nested_object_record_path(self):
133+
# GH 22706
134+
data = {'state': 'Florida',
135+
'info': {
136+
'governor': 'Rick Scott',
137+
'counties': [{'name': 'Dade', 'population': 12345},
138+
{'name': 'Broward', 'population': 40000},
139+
{'name': 'Palm Beach', 'population': 60000}]}}
140+
result = json_normalize(data, record_path=["info", "counties"])
141+
expected = DataFrame([['Dade', 12345],
142+
['Broward', 40000],
143+
['Palm Beach', 60000]],
144+
columns=['name', 'population'])
145+
tm.assert_frame_equal(result, expected)
146+
132147
def test_more_deeply_nested(self, deep_nested):
133148

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

0 commit comments

Comments
 (0)