Skip to content

BUG: DataFrame.to_json OverflowError with np.long* dtypes #55403

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
3 tasks done
nmacholl opened this issue Oct 4, 2023 · 2 comments · Fixed by #55495
Closed
3 tasks done

BUG: DataFrame.to_json OverflowError with np.long* dtypes #55403

nmacholl opened this issue Oct 4, 2023 · 2 comments · Fixed by #55495
Assignees
Labels
Bug DataFrame DataFrame data structure Dtype Conversions Unexpected or buggy dtype conversions IO JSON read_json, to_json, json_normalize

Comments

@nmacholl
Copy link

nmacholl commented Oct 4, 2023

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 sys
import pandas as pd
import numpy as np

df = pd.DataFrame(
    np.random.randint(0, 100, size=(10, 1)),
    dtype=np.longdouble,
)

df.to_json(sys.stdout)

Issue Description

Calling DataFrame.to_json() on a DataFrame with a dtype=np.longdouble or dtype=np.longfloat causes an OverflowError: Maximum recursion level reached error. This seems to be the result of the JSON serializer not handling these types; DataFrame.to_csv() is not affected by this issue.

Expected Behavior

These types should be serialized as floats, or at a minimum, the recursion error should be fixed and a more appropriate serialization error should be raised.

Installed Versions

INSTALLED VERSIONS

commit : e86ed37
python : 3.10.9.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : None
LOCALE : en_US.UTF-8

pandas : 2.1.1
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.1.2
pip : 23.2.1
Cython : None
pytest : 7.4.2
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
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.21.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@nmacholl nmacholl added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 4, 2023
@paulreece paulreece added Dtype Conversions Unexpected or buggy dtype conversions IO JSON read_json, to_json, json_normalize DataFrame DataFrame data structure and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 5, 2023
@paulreece
Copy link
Contributor

I can confirm this bug occurs on the main development branch.

>>> df = pd.DataFrame(
...     np.random.randint(0, 100, size=(10, 1)),
...     dtype=np.longdouble,
... )
>>> df.to_json(sys.stdout)
Traceback (most recent call last):
...
OverflowError: Maximum recursion level reached
>>> df = pd.DataFrame(
...     np.random.randint(0, 100, size=(10, 1)),
...     dtype=np.longfloat,
... )
>>> df.to_json(sys.stdout)
Traceback (most recent call last):
...
OverflowError: Maximum recursion level reached

@gupta-paras
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DataFrame DataFrame data structure Dtype Conversions Unexpected or buggy dtype conversions IO JSON read_json, to_json, json_normalize
Projects
None yet
3 participants