TypeError:slice indices must be integers or None or have an __index__ method
エラー再現手順
データ作成
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
print(a.shape)
print(type(a))
[[1 2 3] [4 5 6]] (2, 3) <class 'numpy.ndarray'>
- サイズ(2,3)のndarray(NumPy配列)を作成
正常パターン
print(a[0:1,0:2])
[[1 2]]
- 抽出したい範囲を数値で指定
エラー再現
print(a[0:1,0:'2'])
TypeError: slice indices must be integers or None or have an __index__ method
- 1か所文字列になっている場合
- エラー発生
対処方法
- 文字列ではなく数値を指定する
- 特にpandasデータの場合
- 表示だけではデータ型まで分からない
- CSV読み込み、DataFrame結合、型変換部分の見直しから問題個所を特定する
