Skip to content

BUG: Fix json_normalize throwing TypeError when record_path has a sequence of dicts #22706 #22804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
- Bug in :meth:`DataFrame.to_dict` when the resulting dict contains non-Python scalars in the case of numeric data (:issue:`23753`)
- :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`)
- 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`)
- Bug in :func:`json_normalize` that caused it to raise ``TypeError`` when two consecutive elements of ``record_path`` are dicts (:issue:`22706`)

Plotting
^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/io/json/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def _pull_field(js, spec):
meta_keys = [sep.join(val) for val in meta]

def _recursive_extract(data, path, seen_meta, level=0):
if isinstance(data, dict):
data = [data]
if len(path) > 1:
for obj in data:
for val, key in zip(meta, meta_keys):
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ def test_value_array_record_prefix(self):
expected = DataFrame([[1], [2]], columns=['Prefix.0'])
tm.assert_frame_equal(result, expected)

def test_nested_object_record_path(self):
# GH 22706
data = {'state': 'Florida',
'info': {
'governor': 'Rick Scott',
'counties': [{'name': 'Dade', 'population': 12345},
{'name': 'Broward', 'population': 40000},
{'name': 'Palm Beach', 'population': 60000}]}}
result = json_normalize(data, record_path=["info", "counties"])
expected = DataFrame([['Dade', 12345],
['Broward', 40000],
['Palm Beach', 60000]],
columns=['name', 'population'])
tm.assert_frame_equal(result, expected)

def test_more_deeply_nested(self, deep_nested):

result = json_normalize(deep_nested, ['states', 'cities'],
Expand Down