Giiovanna
Giiovanna

Reputation: 442

Resample only within the same day

I have a dataframe containing an asset price (OHLC asset data) such that its index is a datetime. The data shoud be sample in minutes but my dataset have some missing minutes.

enter image description here

To fill the missing data, I just used the function

df.resample("1Min").asfreq()

It fills correctly the minutes within one day, but since my dataframe contains data for different days, it usually fills the minutes until midnight of each day. I would like that, if one specific day contains data from 13:05pm to 20:50pm, it fills minute only between those times, i.e., do not exceed those bounds.

I looked at the documentation for the resample function and couldn't find how it can be done. I appreciate your help!

Upvotes: 2

Views: 484

Answers (1)

jezrael
jezrael

Reputation: 862591

I believe you need DataFrame.groupby by days and chain DataFrameGroupBy.resample with Resampler.first:

df = df.groupby(df.index.date).resample("1Min").first()

Upvotes: 2

Related Questions