Pandas 在大熊猫中,非TRIVAL groupby速度较慢

Pandas 在大熊猫中,非TRIVAL groupby速度较慢,pandas,pandas-groupby,Pandas,Pandas Groupby,有很多慢熊猫群发帖子,但它们似乎在某种程度上有所不同,我不知道如何将其转化为我的问题 让我们从我能解决的问题的一个简单版本开始,然后从那里开始构建 (1) 通过每5个时间戳求和col1得到Bin时间序列数据,并创建ohlc条: import pandas as pd import random # set seed in case reproducability becomes useful in the future random.seed(13) # create a weeks wor

有很多慢熊猫群发帖子,但它们似乎在某种程度上有所不同,我不知道如何将其转化为我的问题

让我们从我能解决的问题的一个简单版本开始,然后从那里开始构建

(1) 通过每5个时间戳求和
col1
得到Bin时间序列数据,并创建ohlc条:

import pandas as pd
import random

# set seed in case reproducability becomes useful in the future
random.seed(13)

# create a weeks worth time points 
# NOTE: this is evenly spaced but in real life is not (I can change make this more realistic if someone thinks it's important)
periods = 7 * 24 * 60
time_range = pd.date_range('2016-07-01', periods=periods, freq='T')
df = pd.DataFrame({'col1': [random.random() for _ in range(len(time_range))], 'col2': [random.randint(1, 10) * random.random() for _ in range(len(time_range))]}, index = time_range)

# pandas has some great methods that do things really fast. For example grouping every 5 time stamps and putting into ohlc bars can be done with
df.reset_index(inplace = True)
print(df.head())
df['col1'].groupby(df.index // 5).ohlc()
(2) 如果我想添加两列,以便我们知道每个条的开始和结束时间,该怎么办

(3) 此外,如果我们想按更复杂的函数分组呢?例如,是否有一种快速方法为col1创建ohlc条,使每个条包含最小数量的时间戳,从而使col1*col2>的总和=10?我们还想知道开放式和封闭式邮票

以下是我的工作(但非常缓慢的尝试):


有人能帮我提高性能吗?

您可以使用
cumsum
对两个列进行乘法,然后通过数组操作来创建列分组,以移除10以上的值,并重新启动累积和,例如:

#need these 2 arrays for the calculation
arr_mult = (df.col1*df.col2).values
arr = arr_mult.cumsum().copy() 

gr = np.zeros_like(arr)
for i in range(len(arr)-1):
    if arr[i] >= 10:
        # recalculated the rest of the array once above 10
        arr[i:] -= arr[i] - arr_mult[i]
        # put one where a new group should start
        gr[i] = 1

df['groupings'] = gr.cumsum() + 1
然后,为了得到结果,您可以对col1的ohlc进行浓缩,并在列索引上使用first和last:

grouped = pd.concat([ df.groupby('groupings').col1.ohlc(), 
                      df.groupby('groupings').index.agg(['first', 'last'])], axis=1)\
            .rename(columns = {'first': 'open_stamp','last': 'close_stamp'})

print (grouped.head())
               open      high       low     close          open_stamp  \
groupings                                                               
1.0        0.259008  0.685258  0.259008  0.684082 2016-07-01 00:00:00   
2.0        0.849336  0.849336  0.147160  0.225163 2016-07-01 00:03:00   
3.0        0.734024  0.837657  0.014432  0.014432 2016-07-01 00:08:00   
4.0        0.275837  0.949323  0.146710  0.256708 2016-07-01 00:17:00   
5.0        0.849939  0.849939  0.486785  0.486785 2016-07-01 00:27:00   

                  close_stamp  
groupings                      
1.0       2016-07-01 00:02:00  
2.0       2016-07-01 00:07:00  
3.0       2016-07-01 00:16:00  
4.0       2016-07-01 00:26:00  
5.0       2016-07-01 00:28:00  

注意,在代码中,您所谓的close_stamp实际上是下一个组的open_stamp,而我假设您想要此代码获得的当前组的最后一个stamp。我认为它应该比您的代码更有效

我想知道,在X组中,您是希望在累计总和超过10时包含该行,还是希望将该行包含在下一组中。我这样问是因为切片
base\u idx:idx
不包括行
idx
grouped = pd.concat([ df.groupby('groupings').col1.ohlc(), 
                      df.groupby('groupings').index.agg(['first', 'last'])], axis=1)\
            .rename(columns = {'first': 'open_stamp','last': 'close_stamp'})

print (grouped.head())
               open      high       low     close          open_stamp  \
groupings                                                               
1.0        0.259008  0.685258  0.259008  0.684082 2016-07-01 00:00:00   
2.0        0.849336  0.849336  0.147160  0.225163 2016-07-01 00:03:00   
3.0        0.734024  0.837657  0.014432  0.014432 2016-07-01 00:08:00   
4.0        0.275837  0.949323  0.146710  0.256708 2016-07-01 00:17:00   
5.0        0.849939  0.849939  0.486785  0.486785 2016-07-01 00:27:00   

                  close_stamp  
groupings                      
1.0       2016-07-01 00:02:00  
2.0       2016-07-01 00:07:00  
3.0       2016-07-01 00:16:00  
4.0       2016-07-01 00:26:00  
5.0       2016-07-01 00:28:00