[英]Convert index to datetime in pandas
I am trying to convert the index of a Pandas dataframe to datetime using the to_datetime() method but I get an exception. 我正在尝试使用to_datetime()方法将Pandas数据框的索引转换为datetime,但出现异常。
For a toy reproducible example: 对于玩具可复制的示例:
My data: 我的资料:
In json format: 以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}}'
When I try: 当我尝试:
df3.index.to_datetime()
I get the following error message: 我收到以下错误消息:
---------------------------------------------------------------------------
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'
You can't have a DatetimeIndex
that contains non-datetime things. 您的
DatetimeIndex
不能包含非datetime的东西。 My recommendation is to place the rows labeled ['Id', 'Name', 'Organization']
into a MultiIndex
column object. 我的建议是将标记为
['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
You can create an abomination of an index that is of mixed type. 您可以创建混合类型索引的可憎对象。 If it wasn't obvious, I really advise against this.
如果不是很明显,我真的建议您不要这样做。 If you care enough about the type of your index to try and convert it to
Datetime
then you shouldn't do this. 如果您对索引的类型足够在意,可以尝试将其转换为
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.