Skip to content

BUG: np.isnan gives error with dtype object arrays #28602

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
Tontonio3 opened this issue Mar 28, 2025 · 6 comments
Open

BUG: np.isnan gives error with dtype object arrays #28602

Tontonio3 opened this issue Mar 28, 2025 · 6 comments
Labels

Comments

@Tontonio3
Copy link

Describe the issue:

np.isnan does not work with dtype object arrays

Reproduce the code example:

import numpy as np

list = np.array([1, np.nan, np.inf], dtype=object)

if np.any(np.isnan(list)):
    print("Foo")

Error message:

Traceback (most recent call last):
  File "...\test.py", line 5, in <module>
    if np.any(np.isnan(list)):
              ~~~~~~~~^^^^^^
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''

Python and NumPy Versions:

2.2.1
3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)]

Runtime Environment:

No response

Context for the issue:

I was working on another issue when I came across this. I don't know if this is the intended behavior and I couldn't figure out why it was happening so I decided to report.

@Tontonio3
Copy link
Author

I also found that np.isinf gives the same error

Code:

import numpy as np

list = np.array([1, np.nan, np.inf], dtype=object)

if np.any(np.isinf(list)):
    print("Foo")

Error:

Traceback (most recent call last):
  File "...\test.py", line 5, in <module>
    if np.any(np.isinf(list)):
              ~~~~~~~~^^^^^^
TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

@Mohit-Kundu
Copy link

Hi @Tontonio3, can I work on this?

@Tontonio3
Copy link
Author

@Mohit-Kundu Yeah, I'm not currently working on it. But we usually don't assign issues

@mhvk
Copy link
Contributor

mhvk commented Mar 31, 2025

Before starting to implement, it would be good to think through what should happen. To me, it's not quite obvious what the expectations are. E.g., what should np.isnan(np.array([np.nan, "nan", None, {}], dtype=object)) return? I guess in principle one could pass through math.isnan, which would error. Is that desired? Or should it be the equivalent of np.isnan(array.astype(float))? Maybe refusing the temptation to guess is in fact the right thing to do?

@seberg
Copy link
Member

seberg commented Apr 1, 2025

Yeah, there are some old issues on this, which would be good to at least check briefly first, since this is a duplicate.

I don't really like special casing floats and math.isnan converts to float(), although that should typically work for numeric NaN-like values...
My preference would be -- if anything -- to argue clearly that bool(value != value) is a reasonable definition of "NaN-like".

@Tontonio3
Copy link
Author

To me, it's not quite obvious what the expectations are. E.g., what should np.isnan(np.array([np.nan, "nan", None, {}], dtype=object)) return?

You're right, considering that np.nan is a "mask" and the representation in float of things that are not numbers. So the question I ask is "nan" "NaN-like"? Is False "NaN-like"?

I do believe that it should have the same behaviour for a dtype=object array.

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

No branches or pull requests

4 participants