Reputation: 69
Let I have a dataframe with datetime index like this:
date_time open high low close vol
2018-05-13 18:00:00 70.54 70.60 70.42 70.55 2665
2018-05-13 18:15:00 70.55 70.59 70.53 70.58 378
2018-05-13 18:30:00 70.58 70.70 70.57 70.69 1470
2018-05-13 18:45:00 70.68 70.68 70.63 70.65 427
...
2018-05-14 00:00:00 70.46 70.47 70.40 70.41 1276
2018-05-14 00:15:00 70.41 70.45 70.38 70.39 1356
2018-05-14 00:30:00 70.39 70.48 70.39 70.46 1161
2018-05-14 00:45:00 70.46 70.47 70.43 70.46 359
I need two more columns with DAILY High and DAILY low values. I'm trying make this:
df['day_high']= x.resample('D').high.max()
df.day_high = x.day_high.fillna(method='ffill')
It is works perfectly on days, where 00:00:00 datatime exist. Thus, 14.05.2018 I have a value with datetime 00:00:00 and my code works. But 2018-05-13 day began at 18:00 and my code return "NaN" value (I know, why, but I don't know how to write correct code).
Could you help me? Thanks.
Upvotes: 4
Views: 631
Reputation: 862611
I believe need Resampler.transform
:
df['day_high']= df.resample('D').high.transform('max')
df['day_low']= df.resample('D').low.transform('min')
print (df)
open high low close vol day_high day_low
date_time
2018-05-13 18:00:00 70.54 70.60 70.42 70.55 2665 70.70 70.42
2018-05-13 18:15:00 70.55 70.59 70.53 70.58 378 70.70 70.42
2018-05-13 18:30:00 70.58 70.70 70.57 70.69 1470 70.70 70.42
2018-05-13 18:45:00 70.68 70.68 70.63 70.65 427 70.70 70.42
2018-05-14 00:00:00 70.46 70.47 70.40 70.41 1276 70.48 70.38
2018-05-14 00:15:00 70.41 70.45 70.38 70.39 1356 70.48 70.38
2018-05-14 00:30:00 70.39 70.48 70.39 70.46 1161 70.48 70.38
2018-05-14 00:45:00 70.46 70.47 70.43 70.46 359 70.48 70.38
Upvotes: 6