使用一个范围创建datetime索引,通过每个datetime插入10行来计算所有NAs



我需要以1 HR为间隔的连续数据,因此我写了这个:

df['datetime'] = df['datetime'].dt.floor('HR')
dates_range = pd.date_range(df.datetime.min(), df.datetime.max(), freq = 'HR')
df.set_index('datetime', inplace = True)
df1 = pd.DataFrame(dates_range).set_index(0)
df2 = df.join(df).reset_index()
df2.rename({'index' : 'datetime'}, axis = 1, inplace = True)

对于缺失的日期时间,我希望为每个HR插入100行,并随机填充具有相同数据框架中完整数据的行,而不更改NA行的日期时间。我该怎么做呢?

2021-06-10 08:00:00   Shirt        clothes        800
2021-06-10 08:00:00   Potatoes     food           900
2021-06-10 08:00:00   Forks        cutlery        700
...
2021-06-10 09:00:00    ''          ''             ''
...
2021-07-23 23:00:00   Fancy Feast  animal food    900

尝试这种方法(代码不准确,但应该给一个想法):

for line in data:
last_ts = this_ts # save the previous loop's value
this_ts = datetime.strptime(line[i][:19], date_format)
while this_ts - last_ts > timedelta(hours=1):
# copy/print filler/previous line?
last_ts += timedelta(hours=1)

最新更新