8000 BUG: Attempting to use arrays as slices gives an inappropriate error message · Issue #28486 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Attempting to use arrays as slices gives an inappropriate error message #28486

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

Closed
Orisphera opened this issue Mar 12, 2025 · 4 comments
Closed
Labels

Comments

@Orisphera
Copy link

Describe the issue:

When attempting to use arrays as slices, NumPy gives a message that doesn't accurately describe the actual problem.

Reproduce the code example:

import numpy as np

a = np.zeros(10)
i = np.arange(2, 4)
a[i::i] = 1

Error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 4
      2 b = np.ones(s, dtype=bool)
      3 p = np.arange(s)[a]
----> 4 b[p ** 2::p] = False

TypeError: only integer scalar arrays can be converted to a scalar index

Python and NumPy Versions:

1.26.4
3.12.8 (main, Dec 3 2024, 18:42:41) [GCC 13.3.0]

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.12.8 (main, Dec 3 2024, 18:42:41) [GCC 13.3.0]',
'uname': uname_result(system='Linux', node='nixos', release='6.6.30-rt30', version='#1-NixOS SMP PREEMPT_RT Thu May 2 14:32:50 UTC 2024', machine='x86_64')},
{'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',
'AVX512_SPR']}},
{'filepath': '/nix/store/mhd0rk497xm0xnip7262xdw9bylvzh99-gcc-13.3.0-lib/lib/libgomp.so.1.0.0',
'internal_api': 'openmp',
'num_threads': 2,
'prefix': 'libgomp',
'user_api': 'openmp',
'version': None}]

Context for the issue:

I found this issue while playing around with NumPy, and it really confused me. I think people may get equally confused when doing some actual work.

@ngoldbaum
Copy link
Member

What phrasing would have helped you more? IMO the current message describes what the issue is. Keep in mind that some arrays (those with shape ()) can be used as a scalar index:

In [3]: a = np.zeros(10)

In [4]: i = np.array(2)

In [5]: a[i::i] = 1

In [6]: a
Out[6]: array([0., 0., 1., 0., 1., 0., 1., 0., 1., 0.])

In [7]: i.shape
Out[7]: ()

@Orisphera
Copy link
Author

I misread the error message

@Orisphera Orisphera closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2025
@Orisphera
Copy link
Author

Still, I think it would be nice to mention that NumPy can't change multiple slices at once

@ngoldbaum
Copy link
Member

It is possible to do what (I think?) you were trying to do but it requires using advanced indexing.

There's certainly room to make the error message more clearly communicate what the fundamental issue is: when you use the start:stop:step style indexing, NumPy treats the indices as scalars.

Feel free to reopen the issue btw, there's always room to make errors clearer. I was just trying to point out that there are some subtleties.

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

2 participants
0