Skip to content

API: what should float(pd.NA) return? #48864

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
MarcoGorelli opened this issue Sep 29, 2022 · 4 comments
Closed

API: what should float(pd.NA) return? #48864

MarcoGorelli opened this issue Sep 29, 2022 · 4 comments
Labels
API Design NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@MarcoGorelli
Copy link
Member

From #32265, it seems there was broad agreement that pandas should do

In [4]: pd.Series([1, 2, pd.NA], dtype='Float64').to_numpy()
Out[4]: array([ 1.,  2., nan])

Should float(pd.NA) then return np.nan?

xref matplotlib/matplotlib#23991 (comment)

@MarcoGorelli MarcoGorelli added API Design NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Sep 29, 2022
@phofl
Copy link
Member

phofl commented Sep 30, 2022

Just to clarify: The alternative would be to raise for float(pd.NA)? Anything else that should be considered?

@MarcoGorelli
Copy link
Member Author

Yeah that's what currently happens

In [16]: float(pd.NA)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [16], line 1
----> 1 float(pd.NA)

TypeError: float() argument must be a string or a number, not 'NAType'

Not sure what other considerations there are, I'll give this a go and see what happens

@MarcoGorelli
Copy link
Member Author

This breaks

def test_isin_nan_common_float64(self, nulls_fixture):
if nulls_fixture is pd.NaT or nulls_fixture is pd.NA:
# Check 1) that we cannot construct a Float64Index with this value
# and 2) that with an NaN we do not have .isin(nulls_fixture)
msg = "data is not compatible with Float64Index"
with pytest.raises(ValueError, match=msg):
Float64Index([1.0, nulls_fixture])

, though Float64Index is deprecated anyway

@MarcoGorelli
Copy link
Member Author

from the dev call: it's OK for float(pd.NA) to error, conversion to numpy should be handled in __array__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

No branches or pull requests

2 participants