我有下面的熊猫数据帧,其中包含一些5分钟的日内数据。DeltaBetweenClose是开市当天(美国东部时间9:30)的第一个交易栏。你知道吗
time Date symbol DeltaBetweenClose
9:35 2017-07-17 spy NaN
9:40 2017-07-17 spy -1.2
..........................................
..........................................
16:00 2018-07-17 spy 1.7
9:35 2017-07-18 spy NaN
9:40 2017-07-18 spy 0.3
..........................................
..........................................
9:35 2018-07-17 nflx NaN
我正在尝试创建一个列CloseDelta_sd
,该列计算按symbol
分组的DeltaBetweenClose
列的滚动标准偏差,该列查看前面的30个条并计算标准偏差,同时忽略NaN
s。下面的尝试返回所有NaN
s。当DeltaBetweenClose
列的顶部只有一个NaN
时,该操作有效。你知道吗
df['CloseDelta_sd'] = df.groupby('symbol').DeltaBetweenClose.transform(lambda x: x.rolling(30).std())
问题不在于
std
,因为默认情况下它跳过NaN
,而在于rolling
。你知道吗您需要使用^{} 参数:
由于您提供了
int
而不是偏移量,因此最终会有许多NaN
组,解决方法很简单:相关问题 更多 >
编程相关推荐