如果格式为“固定”,则无法进行选择。这会引发异常(访问时间实际上会快得多)。也就是说,您可以直接索引固定格式。
In [39]: df = DataFrame(np.random.randn(1000000,10))
In [40]: df.to_hdf('test.h5','df',mode='w',format='table')
In [41]: def f():
df = pd.read_hdf('test.h5','df')
return df.loc[50001:,0]
....:
In [42]: def g():
df = pd.read_hdf('test.h5','df')
return df.loc[df.index>50000,0]
....:
In [43]: def h():
return pd.read_hdf('test.h5','df',where='index>50000')[0]
....:
In [44]: f().equals(g())
Out[44]: True
In [46]: f().equals(h())
Out[46]: True
In [47]: %timeit f()
10 loops, best of 3: 159 ms per loop
In [48]: %timeit g()
10 loops, best of 3: 127 ms per loop
In [49]: %timeit h()
1 loops, best of 3: 499 ms per loop
肯定会慢一些。但你正在做更多的工作。这是比较布尔索引器与整个数组。如果您阅读整个框架,那么它有很多优点(例如缓存、局部性)。
当然,如果你只是选择一个连续的切片,那么就做
In [59]: def i():
return pd.read_hdf('test.h5','df',start=50001)[0]
....:
In [60]: i().equals(h())
Out[60]: True
In [61]: %timeit i()
10 loops, best of 3: 86.6 ms per loop