将 pandas 中的索引转换为日期时间

use*_*077 5 python indexing datetime pandas

我正在尝试使用 to_datetime() 方法将 Pandas 数据帧的索引转换为日期时间,但出现异常。

对于玩具可重现的示例:

我的数据:

在此输入图像描述

json 格式:

'{"0":{"Id":1,"Name":"Lewis Alexander","Organization":"Nomura Securities International","Dec 2018":3.25,"June 2019":3.25,"Dec 2019":3.0,"June 2020":3.0,"Dec 2020":2.88},"1":{"Id":2,"Name":"Scott Anderson","Organization":"Bank of the West","Dec 2018":3.19,"June 2019":3.5,"Dec 2019":3.47,"June 2020":3.1,"Dec 2020":2.6},"2":{"Id":3,"Name":"Paul Ashworth","Organization":"Capital Economics","Dec 2018":3.25,"June 2019":3.0,"Dec 2019":2.5,"June 2020":2.25,"Dec 2020":2.25},"3":{"Id":4,"Name":"Daniel Bachman","Organization":"Deloitte LP","Dec 2018":3.2,"June 2019":3.4,"Dec 2019":3.5,"June 2020":3.6,"Dec 2020":3.7},"4":{"Id":5,"Name":"Bernard Baumohl","Organization":"Economic Outlook Group","Dec 2018":3.1,"June 2019":3.35,"Dec 2019":3.6,"June 2020":3.9,"Dec 2020":4.2}}'
Run Code Online (Sandbox Code Playgroud)

当我尝试时:

df3.index.to_datetime()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-53-5ff789e24651> in <module>()
----> 1 df3.index.to_datetime()

AttributeError: 'Index' object has no attribute 'to_datetime'
Run Code Online (Sandbox Code Playgroud)

piR*_*red 4

您不能拥有DatetimeIndex包含非日期时间内容的 a 。我的建议是将标记的行放入列对象['Id', 'Name', 'Organization']MultiIndex

df = df.T.set_index(['Id', 'Name', 'Organization']).T
df = df.set_index(pd.to_datetime(df.index))
df

Id                                         1                2                 3              4                      5
Name                         Lewis Alexander   Scott Anderson     Paul Ashworth Daniel Bachman        Bernard Baumohl
Organization Nomura Securities International Bank of the West Capital Economics    Deloitte LP Economic Outlook Group
2018-12-01                              3.25             3.19              3.25            3.2                    3.1
2019-12-01                                 3             3.47               2.5            3.5                    3.6
2020-12-01                              2.88              2.6              2.25            3.7                    4.2
2019-06-01                              3.25              3.5                 3            3.4                   3.35
2020-06-01                                 3              3.1              2.25            3.6                    3.9
Run Code Online (Sandbox Code Playgroud)

您可以创建令人厌恶的混合类型索引。如果不是很明显,我真的建议不要这样做。如果您非常关心索引的类型并尝试将其转换为这样的类型,Datetime那么您不应该这样做。

df.set_index(df.index.map(lambda x: pd.to_datetime(x, errors='ignore')))

                                                   0                 1                  2               3                       4
2018-12-01 00:00:00                             3.25              3.19               3.25             3.2                     3.1
2019-12-01 00:00:00                                3              3.47                2.5             3.5                     3.6
2020-12-01 00:00:00                             2.88               2.6               2.25             3.7                     4.2
Id                                                 1                 2                  3               4                       5
2019-06-01 00:00:00                             3.25               3.5                  3             3.4                    3.35
2020-06-01 00:00:00                                3               3.1               2.25             3.6                     3.9
Name                                 Lewis Alexander    Scott Anderson      Paul Ashworth  Daniel Bachman         Bernard Baumohl
Organization         Nomura Securities International  Bank of the West  Capital Economics     Deloitte LP  Economic Outlook Group
Run Code Online (Sandbox Code Playgroud)