Skip to content

BUG: dtype() handles single-length comma-separated fields as scalar #25429

@Jacob-Stevens-Haas

Description

@Jacob-Stevens-Haas

Describe the issue:

np.dtype("i,") == np.dtype("i") (note the comma). Is this designed? It feels like the former should result in a structure with one field, whereas the latter should result in a scalar. Same as how python handles parentheses in tuples, i.e. (val) is interpreted differently than (val,)

Reproduce the code example:

import numpy as np

arr = np.empty((1,), dtype="i,")
arr[0] = (5,)

Error message:

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: setting an array element with a sequence.

Python and NumPy Versions:

python :'3.10.11 (main, Apr 5 2023, 14:15:30) [GCC 7.5.0]'
numpy: 1.24.1

Runtime Environment:

[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': '/home/jmsh/github/gen-experiments/env/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.21'}]

Context for the issue:

I'm implementing a version of np.argmax that accepts a axis: None or int or tuple of ints in the way that np.max does. But I also unravel the index from an into into a tuple. I store the tuples in an array of dtype as arr.ndim * "i,".

I know there's other ways of constructing a dtype if I manually name the fields "f0", "f1", .... So not a big priority.

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