在熊猫中将索引转换为日期时间
Posted
技术标签:
【中文标题】在熊猫中将索引转换为日期时间【英文标题】:Convert index to datetime in pandas 【发布时间】:2019-01-15 17:24:58 【问题描述】:我正在尝试使用 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'
当我尝试时:
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'
【问题讨论】:
to_datetime
是一个***函数,它不是 Index 对象的方法,所以它应该是 df3.index = pd.to_datetime(df3.index)
OP 有两个故障点。 @EdChum 确定了一个。另一个是当前索引不适合成为DatetimeIndex
,这也需要解决。
【参考方案1】:
您不能拥有包含非日期时间内容的 DatetimeIndex
。我的建议是将标记为['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
【讨论】:
以上是关于在熊猫中将索引转换为日期时间的主要内容,如果未能解决你的问题,请参考以下文章
在熊猫数据框中将不同的日期时间格式转换为 MM/DD/YYYY 格式