我正在尝试将低频数据帧与高频数据帧合并。我希望低频数据显示在新数据帧的每一行中。例如,2018-01-01在低频数据中有1个值。对于高频数据框中当天的每个小时,应该显示一个值。这是我尝试过的:
from datetime import datetime
low_freq = pd.DataFrame(pd.date_range('2018-01-01', periods = 30, freq='D'))
low_freq['DateTime'] =
[datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S') for i in low_freq[0]]
low_freq.set_index('DateTime', inplace=True)
low_freq['col1'] = np.random.randint(5, size=len(low_freq))
low_freq = low_freq.iloc[:,1:]
high_freq = pd.DataFrame(pd.date_range('2018-01-01', periods=24*30, freq='H'))
high_freq['DateTime'] =
[datetime.strptime(str(i), '%Y-%m-%d %H:%M:%S') for i in high_freq[0]]
high_freq.set_index('DateTime', inplace=True)
high_freq['col1'] = np.random.random(len(high_freq))
high_freq = high_freq.iloc[:,1:]
high_freq['low_freq_val'] =
[low_freq[low_freq.index == i.index.date() for i in high_freq.index]