[英]Convert index to datetime in pandas
我正在尝试使用to_datetime()方法将Pandas数据框的索引转换为datetime,但出现异常。
对于玩具可复制的示例:
我的资料:
以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}}'
当我尝试:
df3.index.to_datetime()
我收到以下错误消息:
---------------------------------------------------------------------------
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'
您的DatetimeIndex
不能包含非datetime的东西。 我的建议是将标记为['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
您可以创建混合类型索引的可憎对象。 如果不是很明显,我真的建议您不要这样做。 如果您对索引的类型足够在意,可以尝试将其转换为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.