Skip to main content
 首页 » 编程设计

python之Pandas 面板切片之提高性能

2024年11月01日9qq78292959

全部,

我目前正在使用 pandas 中的面板来保存我的数据源。我的程序是一个简单的回测引擎。这仅供个人娱乐,但是,我一直在优化它。

拖慢整个应用程序的代码如下:

def get_generator(self): 
    first_n = 0 
    last_n = len(self.data_source.major_axis) 
    cur_n = 0 
    indices = self.data_source.major_axis 
    while cur_n < last_n: 
        yield self.data_source.ix[:, indices[first_n:cur_n + 1], :] 
        cur_n += 1 

如您所见,我基本上每时每刻都在世界中生成一个新 View 并将其返回。索引只是日期。

这段代码对于我尝试使用的数据量来说非常慢。

你知道我怎样才能提高这个速度吗?

干杯!

请您参考如下方法:

self.data_source.ix[:, :cur_n, :] 

相当于

self.data_source.ix[:, indices[first_n:cur_n + 1], :] 

但可能会快 2-3 倍:

In [105]: import pandas as pd     
In [106]: import numpy as np 
In [107]: wp = pd.Panel(np.random.randn(2, 1000, 4)) 
 
In [108]: indices = wp.major_axis 
 
In [109]: %timeit wp.ix[:, :499, :] 
10000 loops, best of 3: 65.2 us per loop 
 
In [110]: %timeit wp.ix[:, indices[0:500], :] 
1000 loops, best of 3: 221 us per loop 
 
In [114]: np.allclose(wp.ix[:, :499, :].values, wp.ix[:, indices[0:500], :].values) 
Out[114]: True 

self.data_source.ix[:, :cur_n, :] 使用 basic slicing .它返回原始面板的 View 。请注意,修改 View 也会修改原始面板。

indices[first_n:cur_n + 1] 是 NumPy 的 ndarray 的子类。 Indexing with an ndarray返回一个副本,而不是一个 View 。制作大型数组的副本比返回 View 慢,这可能是大部分速度增益的来源。但是,有时您可能需要一个副本——例如当您想要在不修改原始面板的情况下修改生成的子面板时。


感谢@Jeff 提出使用转置的额外想法。在我的机器上它产生了显着的改进:

In [131]: wpt = wp.transpose(1,0,2) 
 
In [132]: %timeit wpt.ix[:499] 
10000 loops, best of 3: 37.5 us per loop 
 
In [109]: %timeit wp.ix[:, :499, :] 
10000 loops, best of 3: 65.2 us per loop