将 pandas (多)索引解析为日期时间 [英] Parse pandas (multi)index to datetime

查看:120
本文介绍了将 pandas (多)索引解析为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下多索引 df

                x  y
id  date            
abc 3/1/1994  100  7
    9/1/1994   90  8
    3/1/1995   80  9

日期存储为 str 的位置.

Where dates are stored as str.

我想解析日期索引.以下声明

I want to parse date index. The following statement

df.index.levels[1] = pd.to_datetime(df.index.levels[1])

返回错误:

TypeError: 'FrozenList' does not support mutable operations.

推荐答案

如前所述,您必须重新创建索引:

As mentioned, you have to recreate the index:

df.index = df.index.set_levels([df.index.levels[0], pd.to_datetime(df.index.levels[1])])

这篇关于将 pandas (多)索引解析为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆