Skip to content

OverflowError on using to_json to serialize NaN value with type Decimal. #28609

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

Open
ash-ishh opened this issue Sep 25, 2019 · 1 comment
Open
Labels
Bug IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@ash-ishh
Copy link

 import pandas as pd
 from decimal import Decimal

 def handle_nan(value):
     if isinstance(value, Decimal):
         return float(value)
     return value

 temp_dict = {'a': [Decimal(1), Decimal('NaN')]}
 df = pd.DataFrame(temp_dict)
 df_json = df.to_json(default_handler=handle_nan)

Error

OverflowError: Invalid Nan value when encoding double.

Problem Description

Default handler not being called while getting error in serialization.

Expected Output

handle_nan should be called to return float NaN which is serializable.

INSTALLED VERSIONS commit : None python : 3.6.8.final.0 python-bits : 64 OS : Linux OS-release : 4.4.187-1.el7.elrepo.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 0.25.1
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 18.1
setuptools : 40.6.2
Cython : 0.28.2
pytest : 5.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.0.4
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : 0.9.3
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.1.0
sqlalchemy : 1.3.3
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : 1.0.4

@jbrockmendel jbrockmendel added the IO JSON read_json, to_json, json_normalize label Oct 16, 2019
@mroeschke mroeschke added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels May 8, 2020
@bluppfisk
Copy link

+1 We have a use case in which we perform calculations with Decimals in pandas. There is inconsistency between DataFrame.to_json() handling of numpy.nan and decimal.Decimal('NaN'). The latter throws the OverflowError mentioned above. The first one is simply converted to null.

Is this intended behaviour?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants