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: convert_dtypes does not always convert numpy.nan to pd.NA #59961

Closed
3 tasks done
carlocastoldi opened this issue Oct 4, 2024 · 5 comments
Closed
3 tasks done

BUG: convert_dtypes does not always convert numpy.nan to pd.NA #59961

carlocastoldi opened this issue Oct 4, 2024 · 5 comments
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Duplicate Report Duplicate issue or pull request PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint

Comments

@carlocastoldi
Copy link

carlocastoldi commented Oct 4, 2024

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

x = pd.Series([np.nan, 0.0, 1.2, pd.NA]).convert_dtypes()
y = pd.Series([np.nan, 0.0, 30.2, 10]).convert_dtypes()
z = pd.Series([np.nan, 0.0, 15.2, 9.2]).convert_dtypes()
(z**2 / (x*y)).convert_dtypes()

Issue Description

numpy.nan resulting from an arithmetic operation (e.g., division by zero) is not being converted to pd.NA. The above examples outputs:

0        <NA>
1         NaN
2    6.375276
3        <NA>
dtype: Float64

Expected Behavior

I expect all np.nan present in the Series are converted to pd.NA:

0        <NA>
1        <NA>
2    6.375276
3        <NA>
dtype: Float64

Installed Versions

INSTALLED VERSIONS

commit : 139def2
python : 3.12.3
python-bits : 64
OS : Linux
OS-release : 6.8.0-41-generic
Version : #41-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 2 20:41:06 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 3.0.0.dev0+1545.g139def2145
numpy : 2.2.0.dev0+git20240930.3ee9e6a
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@carlocastoldi carlocastoldi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 4, 2024
@asishm
Copy link
Contributor

asishm commented Oct 4, 2024

Thanks for the report, what you are seeing is discussed in #32265

np.nan gets converted to pd.NA in the x,y,z lines because the dtype of the series before the conversion is numpy dtypes (object, float64, float64 respectively).

However, once you have Float64 Series (which are nullable EA dtypes), convert_dtypes does not have any further effect. It is the pd.NA / pd.NA 0/0 operation which generates the NaN. The current behavior of nullable EA dtypes treats pd.NA as missing and NaN as not a number.

@asishm asishm added Closing Candidate May be closeable, needs more eyeballs PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint labels Oct 4, 2024
@saiteja-yadav
Copy link

Ya! I want to solve this...

@carlocastoldi
Copy link
Author

Mmh okay, i understand the reasoning of using two different symbols for identifying missing data and invalid results.
A small clarification: pd.NA / pd.NA correctly generates another pd.NA. It's 0/0 that generates np.NaN.

That said: is there an "efficient" way for substitute NaNs with pd.NA? I can only think about:

series[np.isnan(series)] = pd.NA

@asishm
Copy link
Contributor

asishm commented Oct 4, 2024

A small clarification: pd.NA / pd.NA correctly generates another pd.NA. It's 0/0 that generates np.NaN.

Yup, thanks!

That said: is there an "efficient" way for substitute NaNs with pd.NA? I can only think about:

series[np.isnan(series)] = pd.NA

There is a suggestion in that thread - #32265 (comment) to add a nan_is_null keyword to fillna/isna which I hope gives a good way to address it in the future

@carlocastoldi
Copy link
Author

Thank you! I just read that thread. Very interesting!

@rhshadrach rhshadrach added Duplicate Report Duplicate issue or pull request and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Duplicate Report Duplicate issue or pull request PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint
Projects
None yet
Development

No branches or pull requests

4 participants