熊猫日期时间切片操作的优化

时间:2020-04-19 20:12:30

标签: python pandas

我正在尝试优化切片范围

for row in tempDF.itertuples(index=True):
        # x and y come from somewhere else
        tp = tempDF.iloc[x:y]

        # created a vector of boolean based on
        # https://datascience.stackexchange.com/questions/23264/

        condition = (
            (tp['date_minute'].dt.date == row.Index.date()) &
            (tp['date_minute'] > row.Index) &
            (pd.Timestamp('10:30').time() <= tp['date_minute'].dt.time) &
            (tp['date_minute'].dt.time <= pd.Timestamp('17:35').time())
        )

        sliced = tp[condition]

使用line_profiler扩展,在condition行上花费了较大代码的大约17%的代码执行时间。这相当于大约2秒。可以想象,对于30,000行的数据帧,执行时间总计为12秒,因此我正在尝试尽可能地优化。

有没有一种方法可以减少在此区块中花费的时间?

谢谢。

0 个答案:

没有答案