在熊猫中按时差移动

时间:2018-12-15 10:39:47

标签: python pandas datetime time-series

我有一个这样的数据框:

                             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秒增量。我试过了,但返回NaN。

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

1 个答案:

答案 0 :(得分:1)

感谢@RishitSanmukhani 两年前提供的这个非常有用的答案:

df['Shifted_Ask'] = df.Ask.reindex(dd.index + pd.Timedelta(seconds=1), method='ffill').values

重新索引参考: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html