Pandas datetime multiindex 更改为 date index 和 time columns (with reindex)

Pandas datetime multiindex changed to date index and time columns (with reindex)

设置: 我有一个像这样的多索引数据框 data;

                                                     value
date                      date                               
2015-08-13 00:00:00+10:00 2015-08-13 06:30:00+10:00  0.812689
                          2015-08-13 15:30:00+10:00  0.054290
                          2015-08-13 16:00:00+10:00  0.206277
                          2015-08-13 16:30:00+10:00  0.082520
                          2015-08-13 17:00:00+10:00  0.009448
                          2015-08-13 17:30:00+10:00  0.000000
2015-08-14 00:00:00+10:00 2015-08-14 06:30:00+10:00  0.000000
                          2015-08-14 07:00:00+10:00  0.000280
                          2015-08-14 07:30:00+10:00  0.034119
                          2015-08-14 08:00:00+10:00  0.168524
                          2015-08-14 08:30:00+10:00  0.471783
                          2015-08-14 09:00:00+10:00  0.522409

作为临时步骤,我将第一个索引级别设置为日期,将第二个索引级别设置为时间,我已经完成了,

# set index level 0 to dates
day_start=[i.date() for i in data.index.levels[0]]
data.index.set_levels(day_start, level=0, inplace=True)

# set index level 1 to times
interval_start=[i.time() for i in data.index.levels[1]]
data_interval.index.set_levels(interval_start, level=1, inplace=True)

# rename time index
data.index.set_names('time', level=1, inplace=True)

也许不是最好的方法,但它给出了,

                        value
date       time              
2015-08-13 06:30:00  0.812689
           15:30:00  0.054290
           16:00:00  0.206277
           16:30:00  0.082520
           17:00:00  0.009448
           17:30:00  0.000000
2015-08-14 06:30:00  0.000000
           07:00:00  0.000280
           07:30:00  0.034119
           08:00:00  0.168524
           08:30:00  0.471783
           09:00:00  0.522409

问题: 接下来我没能做的是重新索引时间,所以从 00:00 到 23:30 每 30 分钟就有一个索引,用零填充缺失数据。这将使它在每一天都保持一致,可能有不同的 start/end 次数据。即

                     value
date       time              
2015-08-13 00:00:00  0.0
           00:30:00  0.0
              :
           06:30:00  0.812689
           07:00:00  0.0
           07:30:00  0.0
              :
           15:30:00  0.054290
           16:00:00  0.206277
           16:30:00  0.082520
              :
           23:30:00  0.0

每天依此类推。在传递 30 分钟间隔时间的数组时,尝试在 level=1 上重新索引似乎没有效果。不确定这是否是正确的方法。

下一步:我想在之后做的是 data.unstack(level=1) 所以所有时间索引变为列 headers。如果我按原样拆开它,我会得到一个奇怪的重复时间的列混搭(这主要是为什么我首先试图让它们在几天之间保持一致)。像;

            value                                                          
time        06:30:00 15:30:00  16:00:00 16:30:00  17:00:00 17:30:00 06:30:00   
date                                                                           
2015-08-13  0.812689  0.05429  0.206277  0.08252  0.009448      0.0      0.0  
2015-08-14  0.000000  0.00000  0.000000  0.00000  0.000000      0.0      0.0   
2015-08-15  0.000000  0.00000  0.000000  0.00000  0.000000      0.0      0.0
2015-08-16  0.000000  0.00000  0.000000  0.00000  0.000000      0.0      0.0   
2015-08-17  0.000000  0.00000  0.000000  0.00000  0.000000      0.0      0.0

那些日子里有很多数据丢失,所以我猜它没有进入正确的列。我可能在重建索引时从根本上遗漏了一些东西,也许我的整个方法并不是获得最终结果的方法。

首先,只需丢弃 "date" 列。这是多余的,弊大于利。那是 df.index = df.index.droplevel(0).

现在你有这个:

                        value
time                         
2015-08-13 06:30:00  0.812689
2015-08-13 15:30:00  0.054290
2015-08-13 16:00:00  0.206277
2015-08-13 16:30:00  0.082520
2015-08-13 17:00:00  0.009448
2015-08-13 17:30:00  0.000000
2015-08-14 06:30:00  0.000000
2015-08-14 07:00:00  0.000280
2015-08-14 07:30:00  0.034119
2015-08-14 08:00:00  0.168524
2015-08-14 08:30:00  0.471783
2015-08-14 09:00:00  0.522409

然后,df.resample('30min').first().fillna(0)

                        value
time                         
2015-08-13 06:30:00  0.812689
2015-08-13 07:00:00  0.000000
2015-08-13 07:30:00  0.000000
2015-08-13 08:00:00  0.000000
...

现在将索引拆分为单独的日期和时间部分:

df['date'] = df.index.date
df['time'] = df.index.time

最后,枢轴:

df.pivot(values='value', index='date', columns='time')