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: series.to_numpy does not work well with pd.Float64Dtype #40630

Closed
3 tasks done
jaspersival opened this issue Mar 25, 2021 · 2 comments · Fixed by #55058
Closed
3 tasks done

BUG: series.to_numpy does not work well with pd.Float64Dtype #40630

jaspersival opened this issue Mar 25, 2021 · 2 comments · Fixed by #55058
Labels
Bug Compat pandas objects compatability with Numpy or Python functions NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@jaspersival
Copy link

jaspersival commented Mar 25, 2021

  • 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
from scipy.stats import norm

series = pd.Series([2.0, -2.5, pd.NA], dtype=pd.Float64Dtype())
norm.pdf(series)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\XXX\AppData\Local\pypoetry\Cache\virtualenvs\XXX-w19Rd76b-py3.9\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1837, in pdf
    cond1 = self._support_mask(x, *args) & (scale > 0)
  File "C:\Users\XXX\AppData\Local\pypoetry\Cache\virtualenvs\XXX-w19Rd76b-py3.9\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 964, in _support_mask
    return (a <= x) & (x <= b)
  File "pandas\_libs\missing.pyx", line 360, in pandas._libs.missing.NAType.__bool__
TypeError: boolean value of NA is ambiguous

Problem description

My issue is about that pandas does not convert a pd.Series with dtype=pd.Float64Dtype() correctly to a numpy array when I try to use the scipy.stats.norm function. Therefore it cannot handle empty values (pd.NA) and the series needs to be cast right now as a float dtype to properly work which is not ideal. It probably has to do with the fact that the method series.to_numpy is called which does not work well with pd.Float64Dtype.

See also [https://github.com/scipy/scipy/issues/13729]

Expected Output

array([0.05399097, 0.0175283 , nan])

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : 1.4.2
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@jaspersival jaspersival added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2021
@mzeitlin11
Copy link
Member

Thanks for the report @jaspersival! This is not limited to just the new nullable float type - the same issue occurs for something like

series = pd.Series([1, -2, pd.NA], dtype="Int64")
norm.pdf(series)

Not sure about the best solution here - we'd like to be consistent for cases like these, but converting the nullable int type to float is problematic since not all ints can be represented by a float. See also #32265, which might be relevant in terms of the choice made here.

@mzeitlin11 mzeitlin11 added Compat pandas objects compatability with Numpy or Python functions NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2021
@taytzehao taytzehao removed their assignment May 20, 2021
@jbrockmendel
Copy link
Member

My issue is about that pandas does not convert a pd.Series with dtype=pd.Float64Dtype() correctly to a numpy array when I try to use the scipy.stats.norm function

In this case I think it converts to a numpy array, just not a useful one. np.array(series) returns an object-dtype Series that contains a pd.NA object. (even if you didnt have an NA entry, it would convert to object dtype and raise a little bit later in the norm.pdf call)

If getting pd.NA rather than np.nan is not important to your use case, then your best bet is to just not use Float64Dtype. Otherwise, i guess we'd need to work with scipy to make norm.pdf something like a ufunc that FloatingArray could then deal with via __array_ufunc__ maybe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
4 participants