python - 为什么 ndarray 允许浮点索引

标签 python numpy

我可以知道为什么 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/

相关文章:

python-3.x - 如何通过比较两列对数据框进行排序

python - 在 matplotlib.pyplot 中绘图时如何显示日期?

python - 一次指挥大量敌人

python - 如何使用numpy进行元素明智的矩阵乘法

python - 如何查找 Python ctypes 库中的泄漏

python - Numpy 八倍精度 float 和 128 位整数。为什么以及如何?

python - numpy 中结构化数组的形状

python - np.array 的 np.array 的深拷贝

python - 编写用于检查新用户创建的 Django 注册表单测试

python - 按值对字典进行排序,然后将排序后的值附加到文本文件中