我可以知道为什么 ndarray 允许浮点索引访问,那是什么意思?
>>> wk1 = numpy.arange(10)
>>> wk1[1:2.8]
array([1])
>>> wk1 = [1,2,3,4,5,6,7,8,9,10]
>>> wk1[1:2.8]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method
>>>
最佳答案
从 1.12 版开始,不再允许在 ndarray 中使用浮点索引并引发错误。
IndexError: only integers, slices (`:`), ellipsis (`...`),
numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Indexing with floats will raise IndexError, e.g., a[0, 0.0]. (See 1.11 release notes)
Indexing with floats raises IndexError, e.g., a[0, 0.0]. (See 1.12 release notes)
(我的重点)
关于python - 为什么 ndarray 允许浮点索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8514547/