I have a dataframe that looks like the below. I want to create a new column titled ‘Daily Change’ which will be the result of the first ‘Open’ price of each day divided by the ‘Open’ price for each 5 minute interval. Once the dates changes I need to the first open price of that day and so on. This is a sample and i want to be able to handle more than two days.
JavaScript
x
14
14
1
Datetime Open High Low
2
2022-10-31 09:30:00 386.4 387.0 386.1
3
2022-10-31 09:35:00 386.9 387.5 386.5
4
2022-10-31 09:40:00 387.1 387.4 386.6
5
2022-10-31 09:45:00 387.1 387.1 385.8
6
2022-10-31 09:50:00 385.8 386.1 385.5
7
2022-10-31 09:55:00 386.0 386.1 385.5
8
2022-11-01 09:30:00 390.1 390.4 389.6
9
2022-11-01 09:35:00 389.9 390.2 389.4
10
2022-11-01 09:40:00 389.9 390.2 389.5
11
2022-11-01 09:45:00 389.7 389.7 388.1
12
2022-11-01 09:50:00 388.4 388.5 387.7
13
2022-11-01 09:55:00 388.0 388.3 387.9
14
I tried the below but get a response stating that the truth value is ambiguous.
JavaScript
1
9
1
days = pd.Series(data.index.date).unique()
2
for day in days:
3
temp_df = data[data.index.date == day]
4
price_df = temp_df[temp_df.index.time == datetime.time(9, 30)]
5
print(price_df)
6
if price_df.index.date == temp_df.index.date:
7
temp_df['Return'] = (temp_df['Open'] / price_df['Open'] - 1)
8
9
Advertisement
Answer
You can use resample_first
to get the first opening value per day then broadcast the value along the rows:
JavaScript
1
18
18
1
# Convert to DatetimeIndex if needed
2
df['Datetime'] = pd.to_datetime(df['Datetime'])
3
df['Daily Change'] = df['Open'] / df.resample('D', on='Datetime')['Open'].transform('first')
4
5
Datetime Open High Low Daily Change
6
0 2022-10-31 09:30:00 386.4 387.0 386.1 1.000000
7
1 2022-10-31 09:35:00 386.9 387.5 386.5 1.001294
8
2 2022-10-31 09:40:00 387.1 387.4 386.6 1.001812
9
3 2022-10-31 09:45:00 387.1 387.1 385.8 1.001812
10
4 2022-10-31 09:50:00 385.8 386.1 385.5 0.998447
11
5 2022-10-31 09:55:00 386.0 386.1 385.5 0.998965
12
6 2022-11-01 09:30:00 390.1 390.4 389.6 1.000000
13
7 2022-11-01 09:35:00 389.9 390.2 389.4 0.999487
14
8 2022-11-01 09:40:00 389.9 390.2 389.5 0.999487
15
9 2022-11-01 09:45:00 389.7 389.7 388.1 0.998975
16
10 2022-11-01 09:50:00 388.4 388.5 387.7 0.995642
17
11 2022-11-01 09:55:00 388.0 388.3 387.9 0.994617
18