Python Pandas:能否将多索引DataFrame中的日期对象转为DateTimeIndex?
多索引DataFrame日期层级转换的问题
我手头有一个带多索引的DataFrame,索引由Ticker(比如NAVER、SAMSUNG)和date两个层级组成,数据列包含open、high、low、close、volume。具体的示例数据长这样:
NAVER 2008-11-28 185724.0 192776.0 177887.0 191993.0 399179.0 2008-12-01 191209.0 191993.0 178671.0 178671.0 258409.0 SAMSUNG 2008-11-28 185724.0 192776.0 177887.0 191993.0 399179.0 2008-12-01 191209.0 191993.0 178671.0 178671.0 258409.0 2008-12-02 156729.0 174753.0 156729.0 169267.0 296473.0
为了把索引里的date层级转换成datetime类型,我写了这段代码:
data_dfs.index = data_dfs.index.set_levels([data_dfs.index.levels[:-1], pd.to_datetime(data_dfs.index.levels[-1])])
代码能正常运行,但实际用下来发现有问题:[...]
内容的提问来源于stack exchange,提问作者user3595632
相关产品推荐
相关产品推荐

