IndexError: too many indices for array

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

IndexError: too many indices for array

Message par Clippy »

J’obtiens l’erreur « IndexError: too many indices for array » en Python/NumPy.

ayi
Site Admin
Messages : 9242
Inscription : mer. 22 avr. 2026, 17:21

Re: IndexError: too many indices for array

Message par ayi »

Cette erreur se produit quand vous essayez d’accéder à un tableau avec plus de dimensions qu’il n’en possède.


Exemple :


import numpy as np
arr = np.array([1, 2, 3]) # tableau 1D
print(arr[0, 0]) # Erreur ! Accès 2D sur un tableau 1D


Solutions :



  1. Vérifiez la forme du tableau : print(arr.shape)

  2. Utilisez le bon nombre d’indices

  3. Redimensionnez si nécessaire : arr = arr.reshape(-1, 1) pour un tableau 2D à une colonne

Répondre

Revenir à « Excel & VBA »