Просто делаем с unnesting после создания списка datetime
df['day']=[pd.date_range(x+' 09:00:00',y+' 08:00:00',freq='H') for x , y in zip(df.startDay,df.endDay)]
yourdf=unnesting(df,['day']).drop_duplicates('day')
yourdf
Out[909]:
day Val endDay startDay
1 1996-03-31 09:00:00 2.20 1996-04-01 1996-03-31
1 1996-03-31 10:00:00 2.20 1996-04-01 1996-03-31
1 1996-03-31 11:00:00 2.20 1996-04-01 1996-03-31
1 1996-03-31 12:00:00 2.20 1996-04-01 1996-03-31
...
Обратите внимание, что я не разделил два столбца с date
и hour
, что можно сделать с помощью yourdf.day.dt.hour; yourdf.dt.date
def unnesting(df, explode):
idx = df.index.repeat(df[explode[0]].str.len())
df1 = pd.concat([
pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
df1.index = idx
return df1.join(df.drop(explode, 1), how='left')