将DatetimeIndex拆分为Pandas中的日期和时间MultiIndex

H. *_*nen 7 python timestamp time-series multi-index pandas

所以,假设我有这样的DatetimeIndex:ed数据(当然会有几天):

                             X      Y       Z
timestamp           
2013-01-02 10:00:13.295000   366    -8242   -1820
2013-01-02 10:00:13.329000   366    -8016   -1820
2013-01-02 10:00:13.352000   32     -8016   -1820
2013-01-02 10:00:13.882000   32     -9250   -1820
2013-01-02 10:00:15.076000  -302    -9250   -1820
Run Code Online (Sandbox Code Playgroud)

我想要像这样的MultiIndexed:

                                 X      Y       Z
Date           Time     
2013-01-02     10:00:13.295000   366    -8242   -1820
               10:00:13.329000   366    -8016   -1820
               10:00:13.352000   32     -8016   -1820
               10:00:13.882000   32     -9250   -1820
               10:00:15.076000  -302    -9250   -1820
Run Code Online (Sandbox Code Playgroud)

我知道你可以(可能)提取DatetimeIndex,将它与.date()和.time()分成两列并将其设置为Dataframe的新索引,但是有更多'pandaic'的方法吗?在我看来,这种功能会派上用场......

Hap*_*001 9

我能想到的最好方法是

In [13]: df.index = pd.MultiIndex.from_arrays([df.index.date, df.index.time], names=['Date','Time'])

In [14]: df
Out[14]: 
                              X     Y     Z
Date       Time                            
2013-01-02 10:00:13.295000  366 -8242 -1820
           10:00:13.329000  366 -8016 -1820
           10:00:13.352000   32 -8016 -1820
           10:00:13.882000   32 -9250 -1820
           10:00:15.076000 -302 -9250 -1820

[5 rows x 3 columns]
Run Code Online (Sandbox Code Playgroud)