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: np.full_like casts float to int for non-finite values #21496

Closed
onnoeberhard opened this issue May 11, 2022 · 4 comments
Closed

BUG: np.full_like casts float to int for non-finite values #21496

onnoeberhard opened this issue May 11, 2022 · 4 comments
Labels

Comments

@onnoeberhard
Copy link

Describe the issue:

Please see the code snippet below. I create an integer array a, then create a new one (b) which is supposed to have the same shape as a, but filled with the float value np.nan. Here, I don't explicitly set the dtype for b. NumPy converts these float values to integer.

This is unexpected behaviour, and I assume is not intended as such. I would have expected either

  1. b is automatically made a float-array (the dtype information from a is not used), or
  2. A TypeError is thrown, notifying me that I am trying to fill an int-array with float values.

Reproduce the code example:

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.full_like(a, np.nan)
>>> b
array([-9223372036854775808, -9223372036854775808, -9223372036854775808])

Error message:

No response

NumPy/Python version information:

1.22.3 3.8.12 (default, Oct 22 2021, 18:39:35)
[Clang 13.0.0 (clang-1300.0.29.3)]

@onnoeberhard
Copy link
Author

I have looked again into the docs and it seems like this behaviour is indeed intended.. But would it ever really make sense in this context? I mean casting non-finite float values to int without even throwing a warning?

@onnoeberhard onnoeberhard changed the title BUG: np.full_like casts float to int BUG: np.full_like casts float to int for non-finite values May 11, 2022
@seberg
Copy link
Member

seberg commented May 11, 2022

See also gh-21437 which would ensure a warning for this example. (Not for a value like 2.5 though, we would have to add a cast-safety check explicitly.)

@seberg
Copy link
Member

seberg commented Jun 14, 2022

This now gives a warning on the main branch (it does not fail, unless np.errstate is used). I am not exactly sure whether we should modify the behavior to be stricter about doing unsafe casts.

I suspect this is a good step in the right direction and the next step would require a larger thought including other functions. So I am tempted to close the issue, but holding off for now.

@seberg
Copy link
Member

seberg commented Jun 14, 2022

Actually, let me close it. Please don't hesitate to reopen an issue though. Xref gh-21437 which added the warnings.

@seberg seberg closed this as completed Jun 14, 2022
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

2 participants