Skip to content

BUG: json_normalize() does not parse floats in scientific notation #37934

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
2 of 3 tasks
KaiRoesner opened this issue Nov 18, 2020 · 5 comments
Closed
2 of 3 tasks

BUG: json_normalize() does not parse floats in scientific notation #37934

KaiRoesner opened this issue Nov 18, 2020 · 5 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@KaiRoesner
Copy link

KaiRoesner commented Nov 18, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import json
import pandas as pd

body = '''[
{ "c1": 1, "c2": "1.01" },
{ "c1": 2, "c2": "1E-3" },
{ "c1": 3, "c2": "3.03" }
]'''

df1 = pd.read_json(body)
df2 = pd.json_normalize(json.loads(body))
print(df1, "\n\n", df1.dtypes)
print("\n\n", df2, "\n\n", df2.dtypes)

Problem description

When creating a dataframe from json string input via json.loads() and pd.json_normalize() float values in scientific notation (e.g. "1E-3") are not parsed as floats. When using pd.read_json() they are parsed correctly.

Output:

    c1     c2
0   1  1.010
1   2  0.001
2   3  3.030 

 c1      int64
c2    float64
dtype: object

    c1    c2
0   1  1.01
1   2  1E-3
2   3  3.03 

 c1     int64
c2    object
dtype: object

Expected Output

    c1     c2
0   1  1.010
1   2  0.001
2   3  3.030 

 c1      int64
c2    float64
dtype: object

    c1    c2
0   1  1.010
1   2  0.001
2   3  3.030

 c1      int64
c2    float64
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-1019-gcp
Version : #19-Ubuntu SMP Tue Jun 23 15:46:40 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.4
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.19
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@KaiRoesner KaiRoesner added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 18, 2020
@jreback
Copy link
Contributor

jreback commented Nov 18, 2020

json_normalize doesn’t parse anything
this is all json.loads which makes a dictionary

@jreback
Copy link
Contributor

jreback commented Nov 18, 2020

to get correct dtypes use read_json

@jreback jreback closed this as completed Nov 18, 2020
@KaiRoesner
Copy link
Author

to get correct dtypes use read_json

I can't use read_json because I need to flatten nested columns.

@jreback
Copy link
Contributor

jreback commented Nov 18, 2020

to get correct dtypes use read_json

I can't use read_json because I need to flatten nested columns.

then u will need to some work to preprocess - try using to_numeric

@KaiRoesner
Copy link
Author

KaiRoesner commented Nov 18, 2020

json_normalize doesn’t parse anything
this is all json.loads which makes a dictionary

Hm. But json.loads() doesn't parse either if the floats are represented as strings in the json string. I tried using the parse_float kwarg but it doesn't even get called for these columns. I thought pandas.read_json() and pandas.json_normalize() should behave in the same way in this respect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants