Skip to content

Commit 2dbf9f2

Browse files
committed
Fix #21356: JSON nested_to_record Silently Drops Top-Level None Values
1 parent ab6aaf7 commit 2dbf9f2

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pandas/io/json/normalize.py

-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ def nested_to_record(ds, prefix="", sep=".", level=0):
8080
if level != 0: # so we skip copying for top level, common case
8181
v = new_d.pop(k)
8282
new_d[newkey] = v
83-
elif v is None: # pop the key if the value is None
84-
new_d.pop(k)
8583
continue
8684
else:
8785
v = new_d.pop(k)

pandas/tests/io/json/test_normalize.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,16 @@ def test_non_ascii_key(self):
238238
tm.assert_frame_equal(result, expected)
239239

240240
def test_missing_field(self, author_missing_data):
241-
# GH20030: Checks for robustness of json_normalize - should
242-
# unnest records where only the first record has a None value
241+
# GH20030: Checks for robustness of json_normalize
243242
result = json_normalize(author_missing_data)
244243
ex_data = [
245-
{'author_name.first': np.nan,
244+
{'info': np.nan,
245+
'author_name.first': np.nan,
246246
'author_name.last_name': np.nan,
247247
'info.created_at': np.nan,
248248
'info.last_updated': np.nan},
249-
{'author_name.first': 'Jane',
249+
{'info': None,
250+
'author_name.first': 'Jane',
250251
'author_name.last_name': 'Doe',
251252
'info.created_at': '11/08/1993',
252253
'info.last_updated': '26/05/2012'}
@@ -351,9 +352,8 @@ def test_json_normalize_errors(self):
351352
errors='raise'
352353
)
353354

354-
def test_nonetype_dropping(self):
355-
# GH20030: Checks that None values are dropped in nested_to_record
356-
# to prevent additional columns of nans when passed to DataFrame
355+
def test_nonetype(self):
356+
# GH21356
357357
data = [
358358
{'info': None,
359359
'author_name':
@@ -367,7 +367,8 @@ def test_nonetype_dropping(self):
367367
]
368368
result = nested_to_record(data)
369369
expected = [
370-
{'author_name.first': 'Smith',
370+
{'info': None,
371+
'author_name.first': 'Smith',
371372
'author_name.last_name': 'Appleseed'},
372373
{'author_name.first': 'Jane',
373374
'author_name.last_name': 'Doe',
@@ -395,6 +396,7 @@ def test_nonetype_top_level_bottom_level(self):
395396
}
396397
result = nested_to_record(data)
397398
expected = {
399+
'id': None,
398400
'location.country.state.id': None,
399401
'location.country.state.town.info.id': None,
400402
'location.country.state.town.info.region': None,
@@ -423,6 +425,7 @@ def test_nonetype_multiple_levels(self):
423425
}
424426
result = nested_to_record(data)
425427
expected = {
428+
'id': None,
426429
'location.id': None,
427430
'location.country.id': None,
428431
'location.country.state.id': None,

0 commit comments

Comments
 (0)