Skip to content

BUG: fixed json_normalize for subrecords with NoneTypes (#20030) #20399

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 2 commits into from
Mar 20, 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.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,7 @@ I/O
- :class:`Timedelta` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`, :issue:`9155`, :issue:`19900`)
- Bug in :meth:`pandas.io.stata.StataReader.value_labels` raising an ``AttributeError`` when called on very old files. Now returns an empty dict (:issue:`19417`)
- Bug in :func:`read_pickle` when unpickling objects with :class:`TimedeltaIndex` or :class:`Float64Index` created with pandas prior to version 0.20 (:issue:`19939`)
- Bug in :meth:`pandas.io.json.json_normalize` where subrecords are not properly normalized if any subrecords values are NoneType (:issue:`20030`)

Plotting
^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/json/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def nested_to_record(ds, prefix="", sep=".", level=0):
if level != 0: # so we skip copying for top level, common case
v = new_d.pop(k)
new_d[newkey] = v
if v is None: # pop the key if the value is None
new_d.pop(k)
continue
else:
v = new_d.pop(k)
Expand Down Expand Up @@ -189,7 +191,8 @@ def _pull_field(js, spec):
data = [data]

if record_path is None:
if any(isinstance(x, dict) for x in compat.itervalues(data[0])):
if any([[isinstance(x, dict)
for x in compat.itervalues(y)] for y in data]):
# naive normalization, this is idempotent for flat records
# and potentially will inflate the data considerably for
# deeply nested structures:
Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ def state_data():
'state': 'Ohio'}]


@pytest.fixture
def author_missing_data():
return [
{'info': None},
{'info':
{'created_at': '11/08/1993', 'last_updated': '26/05/2012'},
'author_name':
{'first': 'Jane', 'last_name': 'Doe'}
}]


class TestJSONNormalize(object):

def test_simple_records(self):
Expand Down Expand Up @@ -226,6 +237,23 @@ def test_non_ascii_key(self):
result = json_normalize(json.loads(testjson))
tm.assert_frame_equal(result, expected)

def test_missing_field(self, author_missing_data):
# GH20030: Checks for robustness of json_normalize - should
# unnest records where only the first record has a None value
result = json_normalize(author_missing_data)
ex_data = [
{'author_name.first': np.nan,
'author_name.last_name': np.nan,
'info.created_at': np.nan,
'info.last_updated': np.nan},
{'author_name.first': 'Jane',
'author_name.last_name': 'Doe',
'info.created_at': '11/08/1993',
'info.last_updated': '26/05/2012'}
]
expected = DataFrame(ex_data)
tm.assert_frame_equal(result, expected)


class TestNestedToRecord(object):

Expand Down Expand Up @@ -322,3 +350,28 @@ def test_json_normalize_errors(self):
['general', 'trade_version']],
errors='raise'
)

def test_nonetype_dropping(self):
# GH20030: Checks that None values are dropped in nested_to_record
# to prevent additional columns of nans when passed to DataFrame
data = [
{'info': None,
'author_name':
{'first': 'Smith', 'last_name': 'Appleseed'}
},
{'info':
{'created_at': '11/08/1993', 'last_updated': '26/05/2012'},
'author_name':
{'first': 'Jane', 'last_name': 'Doe'}
}
]
result = nested_to_record(data)
expected = [
{'author_name.first': 'Smith',
'author_name.last_name': 'Appleseed'},
{'author_name.first': 'Jane',
'author_name.last_name': 'Doe',
'info.created_at': '11/08/1993',
'info.last_updated': '26/05/2012'}]

assert result == expected