Skip to content

BUG: zeros_like does not always override dtype #21603

@oscargus

Description

@oscargus

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)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions