Skip to content

BUG: pandas.read_json casts float column to int #58866

Open
@albertvillanova

Description

@albertvillanova

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 io
import json
import pandas as pd

d = [{"col1": 1, "col2": 1.0}, {"col1": 2, "col2": 2.0}]
df = pd.read_json(io.StringIO(json.dumps(d)))
assert df["col1"].dtype == df["col2"].dtype

Issue Description

Although the explicit JSON values are of float type, the corresponding column dtype is of int dtype.

Expected Behavior

If the JSON contains float values, we would expect the corresponding column dtype is float as well.

At least, we should be able to avoid this casting if needed.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-107-generic
Version : #117~20.04.1-Ubuntu SMP Tue Apr 30 10:35:57 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.22.4
pytz : 2021.3
dateutil : 2.8.2
setuptools : 57.0.0
pip : 24.0
Cython : 0.29.24
pytest : None
hypothesis : None
sphinx : 3.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.4
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.30.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.5.0
gcsfs : None
matplotlib : None
numba : 0.58.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.5.0
scipy : 1.11.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

Arrowpyarrow functionalityBugDocsIO JSONread_json, to_json, json_normalize

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions