BUG: DataFrame#to_json
in pandas 2.2.0 outputs Int64
values containing NA
as floats · Issue #57224 · pandas-dev/pandas (original) (raw)
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
df = pd.DataFrame({"a": [1, pd.NA]}, dtype="Int64") json = df.to_json(orient="records", lines=True) print(json)
Output:
{"a":1.0} <- float!
{"a":null}
Issue Description
In pandas 2.1.4, DataFrame#to_json
outputs Int64
integers as JSON integers even if the column contains NA
, as shown below. This is an expected behavior.
pandas 2.1.4
import pandas as pd
df = pd.DataFrame({"a": [1, pd.NA]}, dtype="Int64") json = df.to_json(orient="records", lines=True) print(json)
Output:
{"a":1} <- int
{"a":null}
However, in pandas 2.2.0, DataFrame#to_json
outputs Int64
integers as JSON floats for columns containing NA
, as shown below.
pandas 2.2.0
import pandas as pd
df = pd.DataFrame({"a": [1, pd.NA]}, dtype="Int64") json = df.to_json(orient="records", lines=True) print(json)
Output:
{"a":1.0} <- float!
{"a":null}
Note that even in 2.2.0, Int64
column values that do not contain any NA
are output as JSON integers as expected.
pandas 2.2.0
import pandas as pd
df = pd.DataFrame({"a": [1, 2]}, dtype="Int64") json = df.to_json(orient="records", lines=True) print(json)
Output:
{"a":1} <- int
{"a":2} <- int
This problem also occurs in Series
as follows.
s = pd.Series([1, pd.NA], dtype="Int64") print(s.to_json(orient="records"))
Output:
[1.0,null]
^
float
Expected Behavior
As in pandas 2.1.4, Int64
column values should be output as JSON integers even if they contain NA
.
import pandas as pd
df = pd.DataFrame({"a": [1, pd.NA]}, dtype="Int64") json = df.to_json(orient="records", lines=True) print(json)
Expected output:
{"a":1} <- int
{"a":null}
Installed Versions
INSTALLED VERSIONS
------------------
commit : fd3f57170aa1af588ba877e8e28c158a20a4886d
python : 3.12.1.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Sun Dec 17 22:12:45 PST 2023; root:xnu-8796.141.3.703.2~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.0
numpy : 1.26.3
pytz : 2024.1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.1
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.12.2
gcsfs : 2023.12.2post1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : 0.20.0
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None