Skip to content

BUG: read_json not reading in large ints properly #41107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ I/O
- Bug in :func:`read_sas` raising ``ValueError`` when ``datetimes`` were null (:issue:`39725`)
- Bug in :func:`read_excel` dropping empty values from single-column spreadsheets (:issue:`39808`)
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
- Bug in :func:`read_json` reading large integers incorrectly if dtype is not specified (:issue:`20608`)
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)

Period
Expand Down
17 changes: 11 additions & 6 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,12 +947,17 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
return new_data, True

if data.dtype == "object":

# try float
try:
if len(data) == 0:
data = data.astype("float64")
except (TypeError, ValueError):
pass
else:
try:
data = data.astype("int64")
except (TypeError, ValueError, OverflowError):
try:
# Maybe out of integer range, try float
data = data.astype("float64")
except (TypeError, ValueError, OverflowError):
pass

if data.dtype.kind == "f":

Expand All @@ -965,7 +970,7 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
pass

# don't coerce 0-len data
if len(data) and (data.dtype == "float" or data.dtype == "object"):
if len(data) and data.dtype == "float":

# coerce ints if we can
try:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,13 @@ def test_read_json_large_numbers2(self):
expected = DataFrame(1.404366e21, index=["articleId"], columns=[0])
tm.assert_frame_equal(result, expected)

def test_large_ints_from_json_strings(self, orient):
# GH 20608
expected = DataFrame([9999999999999999, 10000000000000001])
df_temp = expected.copy().astype(str)
result = read_json(df_temp.to_json(orient=orient), orient=orient)
tm.assert_frame_equal(result, expected)

def test_to_jsonl(self):
# GH9180
df = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
Expand Down