I have two dataframes of data each of which has datetime index. One of the datasets has a time resolution of 12.33 minutes, here df1
, and the other has a time resolution of 1 second, here called df2
:
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(minutes))
data_1 = np.random.randint(1, high=100, size=len(seconds))
Channel_1=data*0.1
Channel_2=data
Channel_1_1=data_1*10
Channel_1_2=data_1*100
df1 = pd.DataFrame({'Datetime': minutes, 'Channel 1': Channel_1, 'Channel 2': Channel_2})
df1 = df1.set_index('Datetime')
df2 = pd.DataFrame({'Datetime': seconds, 'Channel 1': Channel_1_1, 'Channel 2': Channel_1_2})
df2 = df2.set_index('Datetime')
Here is what df1
and df2
look like essentially:
I now want to merge these dataframes so that they share the same time resolution. Essentially I want to resample df2
(ideally using mean()
) and then place it in new dataframe alongside the data from df1
with the same datetime index as df1
(i.e. so that the first values from df2
would be the average between df1.index[0]
and df1.index[[1]]
). It would also be interesting to know if it is possible to average based on the midpoint between df1.index[0]
and df1.index[[1]]
. I have been playing with resample
, reindex
and pd.merge_asof
but I just cant seem to find a solution that works.