Skip to content

DEP: Deprecate constructing dtypes from any object having a .dtype attribute #25306

@timhoffm

Description

@timhoffm

Proposed new feature or change:

I suggest to deprecate this feature. This is essentially already alluded in "we could consider deprecating even more" in #13578 (comment).

It seems too permissive (allowing sloppy user code) and complicates NumPy code (e.g. #13003). Users should instead dereference the .dtype attribute explicitly ("explicit is better than implicit"), e.g.

np.array(data, dtype=obj.dtype)
# instead of
np.array(data, dtype=obj)

Also, the current documentation is not exactly correct. It claims

Any type object with a .dtype attribute

but NumPy explicitly disallows this for ndarrays - for good reason, because np.array(data, dtype=other_array) would be semantically sloppy.

OTOH, I can for example do this with a pandas.Series (np.array(data, dtype=series)). IMHO this should not work for pandas.Series or other objects. An additional unwanted side effect is that one can have quite strange comparions due to dtypes comparing True to valid data type specifications

>>> np.dtype('int64') == pd.Series([1, 2])
True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions