簡體   English   中英

將索引轉換為大熊貓中的日期時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM