[英]Split DatetimeIndex into date and time MultiIndex conveniently in Pandas
So let's say I have DatetimeIndex:ed data like this (there would be several days of course): 所以,假设我有这样的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
and I want it MultiIndexed like this: 我想要像这样的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
I know you could (probably) extract the DatetimeIndex, split it with .date() and .time() into two columns and set it as a new index for the Dataframe, but is there a more 'pandaic' way of doing this? 我知道你可以(可能)提取DatetimeIndex,将它与.date()和.time()分成两列并将其设置为Dataframe的新索引,但是有更多'pandaic'的方法吗? It would seem to me that this sort of functionality would come handy...
在我看来,这种功能会派上用场......
The best way I can think of is 我能想到的最好方法是
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.