I am new to python and learning it by doing some basic stock data analysis. Below is the dataframe I am using
date open high ... close volume
0 2010-01-05 09:16:00 5282.00 5283.10 ... 5281.10 94700 NaN
1 2010-01-05 12:16:00 5281.60 5281.60 ... 5278.30 49100 NaN
2 2010-01-05 16:16:00 5278.50 5280.50 ... 5278.80 62550 NaN
3 2010-01-06 09:16:00 5278.80 5279.45 ... 5277.30 64850 NaN
4 2010-01-06 12:16:00 5277.95 5278.00 ... 5276.00 65251 NaN
As you can see its a timeseries where there are different timeslots within a day. So I want to find the prtc_change (Percentage change) open of 2010-01-06 09:16:00
as compared to the close of 2010-01-05 16:16:00
.
how would I calculate it?
This is the kind of output I am looking for:
date open high ... close volume %change
0 2010-01-05 09:16:00 5282.00 5283.10 ... 5281.10 94700
1 2010-01-05 12:16:00 5281.60 5281.60 ... 5278.30 49100
2 2010-01-05 16:16:00 5278.50 5280.50 ... 5278.80* 62550
3 2010-01-06 09:16:00 5278.80* 5279.45 ... 5277.30 64850 0
4 2010-01-06 12:16:00 5277.95 5278.00 ... 5276.00 65251
The %change
column has 0 for 2010-01-05
-close to 2010-01-05 9:16
-open because the open = close (5278.80 == 5278.80) (marked by *).
Note : I have manipulated the data a little bit as I was working on it . below are the codes
import pandas as pd
import datetime
df = pd.read_csv(r'C:\Users\Admin\Desktop\Python files\nifty.txt' , sep = ';' , names = ["dates","open","high","low","close","volume"])
## fomration the date and time
df['dates'] = pd.to_datetime(df['dates'].astype(str) , format='%Y%m%d %H%M%S' )
## splitting the datetime column into date and time
df['date'] = [d.date() for d in df['dates']]
df['time'] = [d.time() for d in df['dates']]
the current dataframe looks like :
dates open high ... volume date time
0 2010-01-05 09:16:00 5282.00 5283.10 ... 94700 2010-01-05 09:16:00
1 2010-01-05 12:16:00 5281.60 5281.60 ... 49100 2010-01-05 12:16:00
2 2010-01-05 16:16:00 5278.50 5280.50 ... 62550 2010-01-05 16:16:00
3 2010-01-06 09:16:00 5278.80 5279.45 ... 64850 2010-01-05 09:16:00
4 2010-01-06 12:16:00 5277.95 5278.00 ... 65251 2010-01-05 12:16:00