步长为非整数的RangeIndex



是否存在像RangeIndex这样的熊猫索引子类,允许非整数步长?比如:

import pandas as pd
pd.RangeIndex(start=0, stop=10, step=.1)  # TypeError: Wrong type <class 'float'> for value 0.1

文档中列出的类似乎不够。IntervalIndex处理任意范围,但不显式存储步长(我打算稍后在我的代码中访问)。

您想要有一个Float64Index,但是没有参数来定义开始/停止/步骤。

Best可能使用numpy.arange:

idx = pd.Index(np.arange(0,10,0.1))

输出(四舍五入显示):

Float64Index([ 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  1.0,  1.0,  1.0,  1.0,  1.0,
1.0,  1.0,  1.0,  1.0,  2.0,  2.0,  2.0,  2.0,  2.0,  2.0,  2.0,
2.0,  2.0,  2.0,  2.0,  3.0,  3.0,  3.0,  3.0,  3.0,  3.0,  3.0,
3.0,  3.0,  4.0,  4.0,  4.0,  4.0,  4.0,  4.0,  4.0,  4.0,  4.0,
4.0,  4.0,  5.0,  5.0,  5.0,  5.0,  5.0,  5.0,  5.0,  5.0,  5.0,
6.0,  6.0,  6.0,  6.0,  6.0,  6.0,  6.0,  6.0,  6.0,  6.0,  6.0,
7.0,  7.0,  7.0,  7.0,  7.0,  7.0,  7.0,  7.0,  7.0,  8.0,  8.0,
8.0,  8.0,  8.0,  8.0,  8.0,  8.0,  8.0,  8.0,  8.0,  9.0,  9.0,
9.0,  9.0,  9.0,  9.0,  9.0,  9.0,  9.0, 10.0, 10.0, 10.0, 10.0,
10.0],
dtype='float64')

最新更新