Skip to content

Commit 1ece957

Browse files
bolkedebruinWillAyd
andcommitted
Fix NoneType error when pulling non existent field
If normalizing a jsonstruct a field can be absent due to a schema change. Co-Authored-By: William Ayd <[email protected]>
1 parent facd756 commit 1ece957

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ I/O
774774
- Bug in :func:`read_json` where default encoding was not set to ``utf-8`` (:issue:`29565`)
775775
- Bug in :class:`PythonParser` where str and bytes were being mixed when dealing with the decimal field (:issue:`29650`)
776776
- :meth:`read_gbq` now accepts ``progress_bar_type`` to display progress bar while the data downloads. (:issue:`29857`)
777-
-
777+
- Bug in :func:`pandas.io.json.json_normalize` where a missing value in the location specified by `record_path` would raise a ``TypeError`` (:issue:`30148`)
778778

779779
Plotting
780780
^^^^^^^^

pandas/io/json/_normalize.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,15 @@ def _recursive_extract(data, path, seen_meta, level=0):
286286
else:
287287
for obj in data:
288288
recs = _pull_field(obj, path[0])
289-
recs = [
290-
nested_to_record(r, sep=sep, max_level=max_level)
291-
if isinstance(r, dict)
292-
else r
293-
for r in recs
294-
]
289+
try:
290+
recs = [
291+
nested_to_record(r, sep=sep, max_level=max_level)
292+
if isinstance(r, dict)
293+
else r
294+
for r in recs
295+
]
296+
except TypeError:
297+
recs = []
295298

296299
# For repeating the metadata later
297300
lengths.append(len(recs))

pandas/tests/io/json/test_normalize.py

+9
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,15 @@ def test_nested_flattening_consistent(self):
463463
# They should be the same.
464464
tm.assert_frame_equal(df1, df2)
465465

466+
def test_nonetype_record_path(self, nulls_fixture):
467+
# see gh-30148
468+
# should not raise TypeError
469+
df1 = json_normalize(
470+
[{"state": "Texas", "info": nulls_fixture}], record_path=["info"]
471+
)
472+
df2 = DataFrame()
473+
tm.assert_equal(df1, df2)
474+
466475

467476
class TestNestedToRecord:
468477
def test_flat_stays_flat(self):

0 commit comments

Comments
 (0)