-
-
Couldn't load subscription status.
- Fork 19.2k
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
d = {1: {"id": 10, 'status': 'AVAL'},
2: {"id": 30, 'status': 'AVAL', 'items': {"id": 12, "size": 20}},
3: {"id": 50, 'status': 'AVAL', 'items': {"id": 13, "size": 30}}}
df = pd.DataFrame.from_dict(d, orient="index")
pd.json_normalize(df['items'].tolist(), max_level=0)Issue Description
json_normalize will raise an AttributeError of "'float' object has no attribute 'values'" when max_level is set as a number. But it works well when max_level is None. The following is the traceback:
Cell In[6], line 8
1 d = {1: {"id": 10, 'status': 'AVAL'},
2 2: {"id": 30, 'status': 'AVAL', 'items': {"id": 12, "size": 20}},
3 3: {"id": 50, 'status': 'AVAL', 'items': {"id": 13, "size": 30}}}
6 df = pd.DataFrame.from_dict(d, orient="index")
----> 8 pd.json_normalize(df['items'].tolist(), max_level=0)
File ~/Projects/newera/.venv/lib/python3.13/site-packages/pandas/io/json/_normalize.py:460, in json_normalize(data, record_path, meta, meta_prefix, record_prefix, errors, sep, max_level)
457 return DataFrame(_simple_json_normalize(data, sep=sep))
459 if record_path is None:
--> 460 if any([isinstance(x, dict) for x in y.values()] for y in data):
461 # naive normalization, this is idempotent for flat records
462 # and potentially will inflate the data considerably for
463 # deeply nested structures:
464 # {VeryLong: { b: 1,c:2}} -> {VeryLong.b:1 ,VeryLong.c:@}
465 #
466 # TODO: handle record value which are lists, at least error
467 # reasonably
468 data = nested_to_record(data, sep=sep, max_level=max_level)
469 return DataFrame(data)
Expected Behavior
json_normalize shall return similarly to the result when max_level=None
Installed Versions
INSTALLED VERSIONS
commit : 9c8bc3e
python : 3.13.8
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Mon Jul 14 11:29:54 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T8122
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : C.UTF-8
pandas : 2.3.3
numpy : 2.2.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : 3.1.2
sphinx : None
IPython : 9.1.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.5
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.11.0
fsspec : 2025.5.1
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.16.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None