8000 BUG: `zeros_like` does not always override `dtype` · Issue #21603 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: zeros_like does not always override dtype #21603

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
oscargus opened this issue May 26, 2022 · 3 comments
Open

BUG: zeros_like does not always override dtype #21603

oscargus opened this issue May 26, 2022 · 3 comments
Labels

Comments

@oscargus
Copy link
Contributor

Describe the issue:

According to the documentation of zeros_like (and related functions), providing dtype will override the provided data type: https://numpy.org/doc/stable/reference/generated/numpy.zeros_like.html

Basically, my interpretation is that zeros(a.shape, dtype=foo) == zeros_like(a, dtype=foo).

This does not seem to be the case for Quantity from astropy (and maybe other array libraries sharing some specific mechanism).

(Which caused some issue in Astropy, see astropy/astropy#13276, from a change in Matplotlib, see matplotlib/matplotlib#22929. But more importantly, zeros_like does not appear to work as stated in the documentation. Not sure if this is a numpy or astropy issue though, but it is the numpy documentation that seems wrong.)

Reproduce the code example:

import numpy as np
import astropy.units as u

a = [1, 2, 3]*u.m

z0 = np.zeros_like(a, dtype=bool)
z1 = np.zeros(a.shape, dtype=bool)
print(repr(z0))
print(repr(z1))

Error message:

No response

NumPy/Python version information:

1.22.3 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:17:03) [MSC v.1929 64 bit (AMD64)]

@oscargus
Copy link
Contributor Author

After checking the code a bit, I realize that maybe the like in zeros_like is similar to the like keyword in zeros? In that case, I guess that the documentation should be clarified to reflect that.

@pllim
Copy link
Contributor
pllim commented May 26, 2022

@seberg
Copy link
Member
seberg commented May 26, 2022

After checking the code a bit, I realize that maybe the like in zeros_like is similar to the like keyword in zeros?

Yes it is. I expect this is fully in astropy's domain. The only thing is that like= is slightly restrictive, because like=[1, 2, 3] will not work (must already be an "array" and not something that would be converted to a NumPy array, like a list; Although that could be modified).

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

3 participants
0