I have a pandas DataFrame, which stores stock price and time, time column's type is pd.datetime.
here is a demo:
import pandas as pd
df = pd.DataFrame([['2022-09-01 09:33:00', 100.], ['2022-09-01 09:33:14', 101.], ['2022-09-01 09:33:16', 99.4], ['2022-09-01 09:33:30', 100.9]], columns=['time', 'price'])
df['time'] = pd.to_datetime(df['time'])
In [11]: df
Out[11]:
time price
0 2022-09-01 09:33:00 100.0
1 2022-09-01 09:33:14 101.0
2 2022-09-01 09:33:16 99.4
3 2022-09-01 09:33:30 100.9
I want to calculate future return in 15s. (first price after 15 second - current price)
which I want is:
In [13]: df
Out[13]:
time price return
0 2022-09-01 09:33:00 100.0 -0.6 // the future price is 99.4, period is 16s
1 2022-09-01 09:33:14 101.0 -0.1 // the future price is 100.9, period is 16s
2 2022-09-01 09:33:16 99.4 NaN
3 2022-09-01 09:33:30 100.9 NaN
I know df.diff can get difference in index, is there any good methods can do this?