numpy / numpy/numpy

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

Open
#25,306 25 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Proposed new feature or change:

I suggest to deprecate this feature. This is essentially already alluded in "we could consider deprecating even more" in https://github.com/numpy/numpy/pull/13578#issuecomment-755400474.

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the dtype documentation and the discussion in NumPy pull request #13578, then trace how dtype construction handles arbitrary objects with a .dtype attribute. Done means the proposed deprecation behavior is defined for affected objects, explicit obj.dtype usage remains supported, and the documentation accurately describes the accepted inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.