Skip to content

json_normalize should be able to accept an empty list #15534

Closed
@rgbkrk

Description

@rgbkrk

Code Sample, a copy-pastable example if possible

In[21]: pandas.io.json.json_normalize([])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-21-1bd834af8a9b> in <module>()
----> 1 pandas.io.json.json_normalize([])

/usr/local/lib/python3.6/site-packages/pandas/io/json.py in json_normalize(data, record_path, meta, meta_prefix, record_prefix)
    791 
    792     if record_path is None:
--> 793         if any([isinstance(x, dict) for x in compat.itervalues(data[0])]):
    794             # naive normalization, this is idempotent for flat records
    795             # and potentially will inflate the data considerably for

IndexError: list index out of range

Problem description

This should probably return an empty dataframe when the list is empty, based on len(data) being 0.

Expected Output

>>> pandas.io.json.json_normalize([])

Empty DataFrame
Columns: []
Index: []

Admittedly, this could be a series too -- I was using this across several collections and noticed I had to code around when some entries were empty.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Darwin
OS-release: 16.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 34.1.0
Cython: None
numpy: 1.12.0
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.2.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8.1
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO JSONread_json, to_json, json_normalize

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions