Repository: pandas-dev/pandas

Issue:
JSON nested_to_record Silently Drops Top-Level None Values
xref https://github.com/pandas-dev/pandas/pull/21164#issuecomment-394510095

`nested_to_record` is silently dropping `None` values that appear at the top of the JSON. This is IMO unexpected and undesirable.

#### Code Sample, a copy-pastable example if possible

```python
In [3]: data = {
   ...:     "id": None,
   ...:     "location": {
   ...:         "country": None
   ...:     }
   ...: }

In [5]: nested_to_record(data)
Out[5]: {'location.country': None}
```
#### Problem description

The top level `None` value should not be dropped but rather preserved along with lower levels for consistency.

#### Expected Output
```python
In [5]: nested_to_record(data)
Out[5]: {'id': None, 'location.country': None}
```

Note this will break a few tests in `pandas/test_normalize.py`

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: ab6aaf73a848a8725a23bb880be5221dd5ef5b3d
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.0.dev0+67.gab6aaf73a
pytest: 3.4.1
pip: 10.0.1
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.1
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.7.0
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.3
fastparquet: 0.1.4
pandas_gbq: 0.4.1
pandas_datareader: None

</details>

JSON nested_to_record Silently Drops Top-Level None Values
xref https://github.com/pandas-dev/pandas/pull/21164#issuecomment-394510095

`nested_to_record` is silently dropping `None` values that appear at the top of the JSON. This is IMO unexpected and undesirable.

#### Code Sample, a copy-pastable example if possible

```python
In [3]: data = {
   ...:     "id": None,
   ...:     "location": {
   ...:         "country": None
   ...:     }
   ...: }

In [5]: nested_to_record(data)
Out[5]: {'location.country': None}
```
#### Problem description

The top level `None` value should not be dropped but rather preserved along with lower levels for consistency.

#### Expected Output
```python
In [5]: nested_to_record(data)
Out[5]: {'id': None, 'location.country': None}
```

Note this will break a few tests in `pandas/test_normalize.py`

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: ab6aaf73a848a8725a23bb880be5221dd5ef5b3d
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.0.dev0+67.gab6aaf73a
pytest: 3.4.1
pip: 10.0.1
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.1
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.7.0
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.3
fastparquet: 0.1.4
pandas_gbq: 0.4.1
pandas_datareader: None

</details>



Discussion:
Hi, 
I'm looking for a good first timer issue. Would like to take this up! 
Thank you. 
Hi, 
I'm looking for a good first timer issue. Would like to take this up! 
Thank you. 

Resolving patch:
diff --git a/pandas/io/json/normalize.py b/pandas/io/json/normalize.py
--- a/pandas/io/json/normalize.py
+++ b/pandas/io/json/normalize.py
@@ -80,8 +80,6 @@ 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
-                elif v is None:  # pop the key if the value is None
-                    new_d.pop(k)
                 continue
             else:
                 v = new_d.pop(k)


Test patch:
diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py
--- a/pandas/tests/io/json/test_normalize.py
+++ b/pandas/tests/io/json/test_normalize.py
@@ -238,15 +238,16 @@ def test_non_ascii_key(self):
         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
+        # GH20030:
         result = json_normalize(author_missing_data)
         ex_data = [
-            {'author_name.first': np.nan,
+            {'info': np.nan,
+             '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',
+            {'info': None,
+             'author_name.first': 'Jane',
              'author_name.last_name': 'Doe',
              'info.created_at': '11/08/1993',
              'info.last_updated': '26/05/2012'}
@@ -351,9 +352,8 @@ def test_json_normalize_errors(self):
                       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
+    def test_donot_drop_nonevalues(self):
+        # GH21356
         data = [
             {'info': None,
              'author_name':
@@ -367,7 +367,8 @@ def test_nonetype_dropping(self):
         ]
         result = nested_to_record(data)
         expected = [
-            {'author_name.first': 'Smith',
+            {'info': None,
+             'author_name.first': 'Smith',
              'author_name.last_name': 'Appleseed'},
             {'author_name.first': 'Jane',
              'author_name.last_name': 'Doe',
@@ -395,6 +396,7 @@ def test_nonetype_top_level_bottom_level(self):
         }
         result = nested_to_record(data)
         expected = {
+            'id': None,
             'location.country.state.id': None,
             'location.country.state.town.info.id': None,
             'location.country.state.town.info.region': None,
@@ -423,6 +425,7 @@ def test_nonetype_multiple_levels(self):
         }
         result = nested_to_record(data)
         expected = {
+            'id': None,
             'location.id': None,
             'location.country.id': None,
             'location.country.state.id': None,