Skip to content
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

BUG: json_normalize() upcasts column with missing values #37935

Closed
2 of 3 tasks
KaiRoesner opened this issue Nov 18, 2020 · 1 comment
Closed
2 of 3 tasks

BUG: json_normalize() upcasts column with missing values #37935

KaiRoesner opened this issue Nov 18, 2020 · 1 comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Usage Question

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 pandas as pd

data = [
  { "c1": 1, "c2": 1, "c3": 1.01 },
  { "c1": 2, "c3": 2.02 },
  { "c1": 3, "c2": 3, "c3": 3.03 }
]

df = pd.json_normalize(data)
print(df, "\n\n", df.dtypes)

Problem description

When creating a dataframe via pd.json_normalize() integer columns are upcast to float if column values are missing for some records even when all other values are consistently integer.

Output:

   c1   c2    c3
0   1  1.0  1.01
1   2  NaN  2.02
2   3  3.0  3.03 

c1      int64
c2    float64
c3    float64

Expected Output

   c1   c2    c3
0   1    1  1.01
1   2  NaN  2.02
2   3    3  3.03 

c1      int64
c2      int64
c3    float64

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
@jbrockmendel jbrockmendel added the IO JSON read_json, to_json, json_normalize label Jun 6, 2021
@mroeschke mroeschke added Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 14, 2021
@simonjayhawkins
Copy link
Member

Thanks @KaiRoesner for the report.

Expected Output

   c1   c2    c3
0   1    1  1.01
1   2  NaN  2.02
2   3    3  3.03 

c1      int64
c2      int64
c3    float64

numpy int64 columns cannot hold a floating np.nan value. This expected result is impossible. It is common in pandas to upcast int64 columns to float64 when there is missing data.

You could try using the experimental nullable integer dtype intended to avoid this issue in future pandas ...

pd.json_normalize(data).astype({"c2": "Int64"})
   c1    c2    c3
0   1     1  1.01
1   2  <NA>  2.02
2   3     3  3.03

or filling the missing values with a sentinel value compatible with the numpy int dtype ...

pd.json_normalize(data).fillna({"c2": 0}).astype({"c2": "int64"})
   c1  c2    c3
0   1   1  1.01
1   2   0  2.02
2   3   3  3.03

closing this issue as not a bug.

There is also an open enhancement suggestion that would allow the specification of the nullable dtypes upfront #33414 and in future pandas the nullable dtypes will probably become the default.

@simonjayhawkins simonjayhawkins added Usage Question Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate and removed Bug labels Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Usage Question
Projects
None yet
Development

No branches or pull requests

4 participants