使用Numba优化熊猫功能

时间:2020-03-29 14:08:59

标签: python pandas optimization numba

此程序采用矩阵格式的时间序列,矩阵的列之一是体积,然后程序压缩并扩展行,以使每一行最终具有相同的体积,同时保持时间顺序。这样做是为了使小批量事件不会在TS计算中增加异方差。由于内存使用率高和运行缓慢,我一直在尝试优化pandas函数。我编写了以下内容,并尝试将其放入Numba中,但numba jit并没有提高性能。 我想知道如何最好地优化此功能,以使其快速运行,并且不会由于内存不足而使计算机崩溃。任何帮助将不胜感激。

@jit((float64[:], int64))
def create_new_df(df):
    ndf = (df.loc[df.index.repeat(df.Volume)]
                .assign(Volume=1,
                        groups=lambda x: np.arange(len(x))//750
                       )
                .groupby('groups')
                .agg({'Timestamp':'first',
                      'Volume':'sum',
                      'C':'mean',
                      'D':'mean',
                      'E':'mean',
                      'F':'mean',
                      'G':'mean'}))

对于len(x)// 750,此(为简洁起见):

Timestamp Volume C          D           E           F         G

23:32:03   1    57.05      57.05       57.05       57.05     57.05

23:32:04   0    57.05      57.05       57.05       57.05     57.05

...

23:32:55   0    57.05      57.05       57.05       57.05     57.05

23:32:56   2    57.04      57.04       57.04       57.04     57.04

23:32:57   0    57.04      57.04       57.04       57.04     57.04

23:32:58   0    57.04      57.04       57.04       57.04     57.04

成为这个(为简洁起见也被删节):

Timestamp Volume    C           D           E           F       G

23:32:03  750   57.10   57.10   57.10   57.10   57.10

09:05:14  750   57.07   57.07   57.07   57.07   57.07

11:26:11  750   57.14   57.14   57.14   57.14   57.14

13:40:21  750   57.12   57.12   57.12   57.12   57.12

00:18:15  750   57.44   57.45   57.44   57.45   57.45

0 个答案:

没有答案