Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Add a np.isnan loop for the object dtype (and possible isfinite, ...) #9009

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

Open
eric-wieser opened this issue Apr 27, 2017 · 7 comments

Comments

@eric-wieser
Copy link
Member

eric-wieser commented Apr 27, 2017

This would fix help fix #9008.

From most to least-well defined

  1. Object arrays of float,np.float32, np.datetime64... → return np.isnan(item)
  2. Object arrays of int,np.int32, ... → return False
  3. Object arrays of any type that implements __float__ or __int__ - return np.isnan(float(x))
  4. Object arrays of np.ndarrayreturn np.isnan(item) (requires a O->O loop, not O->?)
  5. Any value → return x != x

I think I'd draw the line after 3, and have 4 onwards throw an error.

@h-vetinari
Copy link
Contributor

I'd draw the line at 4. nan is a ubiquitous missing-value marker for many downstream libraries (not least pandas), and that may just as well happen in an array of objects. Not 100% sure I understand "Object arrays of np.ndarray", assuming you don't mean nested arrays.

I think it would be very reasonable to have:

arr = np.array([1, '2', {3, '3'}, np.nan])
arr
# array([1, '2', {3, '3'}, nan], dtype=object)
np.isnan(arr)  # <- currently raises
# array([False, False, False,  True])

@eric-wieser
Copy link
Member Author

assuming you don't mean nested arrays.

No, that is what I mean

@h-vetinari
Copy link
Contributor

No, that is what I mean

To be fair, it wasn't clear whether the ellipses in 1. & 2. would include things such as strings, lists, maps, etc. But in any case, then I guess 3 is a good cut-off.

@smithsp
Copy link

smithsp commented Mar 18, 2019

Would this enhancement allow

numpy.isnan(numpy.array([],dtype=object)))

to not fail?

@mattip
Copy link
Member

mattip commented Mar 17, 2020

@eric-wieser is this still relevant?

@eric-wieser
Copy link
Member Author

Yes, I think so. I think #6320 contains a candidate implementation.

@seberg seberg changed the title ENH: Add a np.isnan loop for the object dtype ENH: Add a np.isnan loop for the object dtype (and possible isfinite, ...) Jun 11, 2022
@h-vetinari
Copy link
Contributor

h-vetinari commented Apr 12, 2023

People are bending themselves out of shape to do this. isnan and friends simply shouldn't fail on object, the error message is detrimental IMO:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

because testing for nan's (at least in principle) doesn't have anything to do with having to cast.

Even more so because adding casting="unsafe" (documented option in the signature) changes nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants