You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:15:29