慕娘9325324
将日期时间设置为索引(在转换为日期时间之后),并使用带有前向填充的 asfreq 方法用以前的值填充空值:#thanks to @a_guest for the cleaned sample datadf = pd.DataFrame( data=[['2016-02-24 01:00', 12, 15], ['2016-02-24 02:00', 14, 13], ['2016-02-24 03:00', 8, 12]], columns=['Datetime', 'Level1', 'Level2'])df['Datetime'] = pd.to_datetime(df['Datetime'])df = df.set_index('Datetime')df.asfreq('15min',method='pad') Level1 Level2Datetime 2016-02-24 01:00:00 12 152016-02-24 01:15:00 12 152016-02-24 01:30:00 12 152016-02-24 01:45:00 12 152016-02-24 02:00:00 14 132016-02-24 02:15:00 14 132016-02-24 02:30:00 14 132016-02-24 02:45:00 14 132016-02-24 03:00:00 8 12注意:您的问题是时间间隔为 15 分钟,您的最晚时间是下午 3 点,而不是数据中共享的下午 4 点。
有只小跳蛙
您可以创建原始数据框的副本,其中您Datetime每次将列递增 15 分钟,然后使用pd.concatfollowed bysort_values获得最终结果:from datetime import timedeltaimport pandas as pddf = pd.DataFrame( data=[['2016-02-24 01:00', 12, 15], ['2016-02-24 02:00', 14, 13], ['2016-02-24 03:00', 8, 12]], columns=['Datetime', 'Level1', 'Level2'])df['Datetime'] = pd.to_datetime(df['Datetime'])intervals = [df]while len(intervals) < 4: new = intervals[-1].copy() new['Datetime'] += timedelta(minutes=15) intervals.append(new)result = pd.concat(intervals).sort_values(by='Datetime')