Skip to content

Commit 7ae4fd1

Browse files
rgbkrkjorisvandenbossche
authored andcommitted
BUG: handle empty lists in json_normalize (#15535)
closes #15534
1 parent 04e1168 commit 7ae4fd1

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Other enhancements
192192
- HTML table output skips ``colspan`` or ``rowspan`` attribute if equal to 1. (:issue:`15403`)
193193
- ``pd.TimedeltaIndex`` now has a custom datetick formatter specifically designed for nanosecond level precision (:issue:`8711`)
194194
- ``pd.types.concat.union_categoricals`` gained the ``ignore_ordered`` argument to allow ignoring the ordered attribute of unioned categoricals (:issue:`13410`). See the :ref:`categorical union docs <categorical.union>` for more information.
195+
- ``pandas.io.json.json_normalize()`` with an empty ``list`` will return an empty ``DataFrame`` (:issue:`15534`)
195196

196197
.. _ISO 8601 duration: https://en.wikipedia.org/wiki/ISO_8601#Durations
197198

pandas/io/json/normalize.py

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ def _pull_field(js, spec):
157157

158158
return result
159159

160+
if isinstance(data, list) and len(data) is 0:
161+
return DataFrame()
162+
160163
# A bit of a hackjob
161164
if isinstance(data, dict):
162165
data = [data]

pandas/tests/io/json/test_normalize.py

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def test_simple_normalize(self):
6262

6363
tm.assert_frame_equal(result, expected)
6464

65+
def test_empty_array(self):
66+
result = json_normalize([])
67+
expected = DataFrame()
68+
tm.assert_frame_equal(result, expected)
69+
6570
def test_more_deeply_nested(self):
6671
data = [{'country': 'USA',
6772
'states': [{'name': 'California',

0 commit comments

Comments
 (0)