在大熊猫中按增量时间移动行

2021-09-16 23:55:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个数据帧:

                             Ask      Bid  AskVolume  BidVolume
Gmt time
2018-10-10 10:30:00.573  1.14908  1.14906       1.57       1.00
2018-10-10 10:30:01.044  1.14906  1.14904       1.00       6.67
2018-10-10 10:30:01.620  1.14905  1.14903       1.00       2.06
2018-10-10 10:30:01.934  1.14906  1.14902       1.95       4.69
2018-10-10 10:30:02.093  1.14904  1.14902       1.00       1.50
2018-10-10 10:30:02.422  1.14902  1.14901       1.20       1.25
2018-10-10 10:30:02.967  1.14902  1.14900       3.00       1.50
2018-10-10 10:30:03.485  1.14902  1.14899       1.50       6.75
2018-10-10 10:30:04.016  1.14902  1.14900       1.00       1.25
2018-10-10 10:30:04.584  1.14902  1.14899       1.10       3.94
2018-10-10 10:30:05.316  1.14902  1.14900       1.57       1.00
2018-10-10 10:30:06.457  1.14902  1.14899       1.10       3.00
2018-10-10 10:30:06.972  1.14901  1.14899       1.00       2.25
2018-10-10 10:30:07.606  1.14901  1.14898       2.70       3.00
2018-10-10 10:30:10.751  1.14901  1.14899       1.20       1.87

我想将Ask列移动1秒增量。我试过了,但没成功。你知道吗

df = pd.read_csv('2hour.csv')
delta = pd.Timedelta(1,unit='s')
df['Gmt time'] = pd.DatetimeIndex(df['Gmt time'])
df.set_index(keys='Gmt time',inplace=True)
df['Shifted_Ask'] = df['Ask'].shift(freq=delta)
print df.head()

期望值:

Gmt time                 Ask      Bid           AskVolume  BidVolume   Shifted_Ask 
2018-10-10 10:30:00.573  1.14908  1.14906       1.57       1.00         1.14906          
2018-10-10 10:30:01.044  1.14906  1.14904       1.00       6.67         1.14902  
2018-10-10 10:30:01.620  1.14905  1.14903       1.00       2.06         1.14902 
2018-10-10 10:30:01.934  1.14906  1.14902       1.95       4.69         1.14902
2018-10-10 10:30:02.093  1.14904  1.14902       1.00       1.50         1.14902