简体   繁体   English

具有非整数步长的 RangeIndex

[英]RangeIndex with non-integer step

Is there a pandas Index subclass like RangeIndex that allows non-integer step sizes?是否有像 RangeIndex 这样的熊猫索引子类允许非整数步长? Something like:就像是:

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

The classes listed in the docs do not appear adequate. 文档中列出的类似乎不够用。 IntervalIndex handles arbitrary ranges but does not explicitly store a step size (which I intend to access later in my code). IntervalIndex 处理任意范围,但不明确存储步长(我打算稍后在我的代码中访问)。

You want to have a Float64Index , there is however no parameter to define the start/stop/step.您想要一个Float64Index ,但是没有参数来定义开始/停止/步骤。

Best is likely to use numpy.arange :最好的可能是使用numpy.arange

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

output (rounded for display):输出(四舍五入显示):

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM