Skip to content

Commit 99b745e

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

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
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

+9-4
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ def test_missing_field(self, author_missing_data):
242242
# unnest records where only the first record has a None value
243243
result = json_normalize(author_missing_data)
244244
ex_data = [
245-
{'author_name.first': np.nan,
245+
{'info': np.nan,
246+
'author_name.first': np.nan,
246247
'author_name.last_name': np.nan,
247248
'info.created_at': np.nan,
248249
'info.last_updated': np.nan},
249-
{'author_name.first': 'Jane',
250+
{'info': None,
251+
'author_name.first': 'Jane',
250252
'author_name.last_name': 'Doe',
251253
'info.created_at': '11/08/1993',
252254
'info.last_updated': '26/05/2012'}
@@ -351,7 +353,7 @@ def test_json_normalize_errors(self):
351353
errors='raise'
352354
)
353355

354-
def test_nonetype_dropping(self):
356+
def test_nonetype(self):
355357
# GH20030: Checks that None values are dropped in nested_to_record
356358
# to prevent additional columns of nans when passed to DataFrame
357359
data = [
@@ -367,7 +369,8 @@ def test_nonetype_dropping(self):
367369
]
368370
result = nested_to_record(data)
369371
expected = [
370-
{'author_name.first': 'Smith',
372+
{'info': None,
373+
'author_name.first': 'Smith',
371374
'author_name.last_name': 'Appleseed'},
372375
{'author_name.first': 'Jane',
373376
'author_name.last_name': 'Doe',
@@ -395,6 +398,7 @@ def test_nonetype_top_level_bottom_level(self):
395398
}
396399
result = nested_to_record(data)
397400
expected = {
401+
'id': None,
398402
'location.country.state.id': None,
399403
'location.country.state.town.info.id': None,
400404
'location.country.state.town.info.region': None,
@@ -423,6 +427,7 @@ def test_nonetype_multiple_levels(self):
423427
}
424428
result = nested_to_record(data)
425429
expected = {
430+
'id': None,
426431
'location.id': None,
427432
'location.country.id': None,
428433
'location.country.state.id': None,

0 commit comments

Comments
 (0)