0

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: enter image description here enter image description here

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.

Oxbowerce
  • 430
  • 5
  • 14
user1912925
  • 781
  • 1
  • 11
  • 25
  • [This](https://stackoverflow.com/questions/48253360/merge-series-dataframe-with-different-time-frequencies-in-python) should answer your question – user15270287 Feb 26 '21 at 14:43
  • [This](https://stackoverflow.com/questions/66375748/pandas-group-merge-dataframe-by-non-periodic-series/66376152#66376152) might be a better match to your question. – Quang Hoang Feb 26 '21 at 14:50
  • Thanks @QuangHoang I think you are suggesting something like this 'test = df1.index.to_series().reset_index(drop=True) bucket = pd.cut(df2.index, bins=test, labels=test[:-1],right=False) df2.groupby(bucket).mean()'. I will test and see how I get on, thanks. – user1912925 Feb 26 '21 at 15:22

0 Answers0