使用.resample()和.groupby()问题的熊猫加权平均值

时间:2020-08-09 07:34:13

标签: python pandas pandas-groupby pandas-resample

当尝试使用.resample()方法和.apply()来计算批量加权平均价格时,我遇到了问题。这是设置。我有一个名为All的数据框,其中包含以下信息:

All.head()

                               price     volume buy_sell market_limit misc
dtime                                                                      
2020-08-06 11:26:45.705199957  395.23   0.064363      buy        limit     
2020-08-06 11:26:45.702500105  395.23   0.114847      buy        limit     
2020-08-06 11:26:45.700900078  395.23  30.000000      buy        limit     
2020-08-06 11:26:45.698899984  395.23  11.175000      buy        limit     
2020-08-06 11:26:45.696000099  395.23   2.415115      buy        limit   

All.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 38471 entries, 2020-08-06 11:26:45.705199957 to 2020-08-09 04:20:45.227400064
Data columns (total 5 columns):
 #   Column        Non-Null Count  Dtype  
---  ------        --------------  -----  
 0   price         38471 non-null  float64
 1   volume        38471 non-null  float64
 2   buy_sell      38471 non-null  object 
 3   market_limit  38471 non-null  object 
 4   misc          38471 non-null  object 
dtypes: float64(2), object(3)
memory usage: 1.8+ MB

数据帧具有日期时间索引,因此我可以使用^ resample方法每天对数据进行重新采样。现在,我想使用apply()和np.average()

计算交易量加权平均价格
All.resample('1D').apply(lambda x: np.average(x.price, weights = x.volume))

但是,这将导致以下错误:

AttributeError: 'Series' object has no attribute 'price'

绕开.resample()并使用.groupby()作为替代方法时,它确实起作用。但是,我需要做一些附加的步骤来处理日期,而我真的不想这么做。

All_alt = All.reset_index()
All_alt['dtime'] = All_alt['dtime'].apply(lambda x: x.date())
All_alt.head()

dtime   price     volume buy_sell market_limit misc
0  2020-08-06  395.23   0.064363      buy        limit     
1  2020-08-06  395.23   0.114847      buy        limit     
2  2020-08-06  395.23  30.000000      buy        limit     
3  2020-08-06  395.23  11.175000      buy        limit     
4  2020-08-06  395.23   2.415115      buy        limit    


All_alt.groupby(['dtime']).apply(lambda x: np.average(x.price, weights = x.volume))

dtime
2020-08-06    396.889472
2020-08-07    381.178095
2020-08-08    387.182528
2020-08-09    397.162415
dtype: float64

有人可以使用resample()为我提供正确的语法吗?我不明白为什么在使用apply()时resample()创建一个Series对象

非常感谢。

0 个答案:

没有答案