-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: should to_numpy() by default return the corresponding type, and raise otherwise? #48891
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
Comments
Could you include how you would handle df.values/ser.values? |
I don't think they'd need to change |
Is an "appropriate" |
this looks reasonable to do |
What would
do? I can see a case both for "promote to float" and "raise". |
Thanks for weighing in - I'm suggesting it'd raise. Because |
Numpy is going to error on equality comparisons between In [1]: np.array([1, 3]) == np.array([4, pd.NA])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[1], line 1
----> 1 np.array([1, 3]) == np.array([4, pd.NA])
File ~/pandas-dev/pandas/_libs/missing.pyx:413, in pandas._libs.missing.NAType.__bool__()
411
412 def __bool__(self):
--> 413 raise TypeError("boolean value of NA is ambiguous")
414
415 def __hash__(self):
TypeError: boolean value of NA is ambiguous |
+1 for avoiding object dtype where possible, e.g. at least in these cases:
|
Currently,
to_numpy()
from a nullable type always returnsdtype
object
, regardless of whether the Series contains missing values.E.g.
This isn't great for users/downstream libraries. E.g. matplotlib/matplotlib#23991 (comment)
Users can specify a
dtype
into_numpy()
, but if they want to mirror the current behaviour of pandas whereby one has:then they would need to add extra logic to their code.
I'd suggest that instead,
to_numpy()
just always convert to the correspondingnumpy
type. Then:NA
, thento_numpy()
will just convert to the correspondingnumpy
type:NA
, then unless it's being converted tofloat
(which can handlenan
), it'll raise:This would achieve:
The text was updated successfully, but these errors were encountered: