更改dataframe索引中的日期格式时出错

问题描述 投票:1回答:1

我有以下df:

                  A         B
2018-01-02  100.000000  100.000000
2018-01-03  100.808036  100.325886
2018-01-04  101.616560  102.307700

我期待着改变索引的时间格式,所以我尝试了(在链接Format pandas dataframe index date中使用@jezrael的响应):

df.index = rdo.index.strftime('%d-%m-%Y')

但它输出:

AttributeError: 'Index' object has no attribute 'strftime'

我想要的输出是:

                 A         B
02-01-2018  100.000000  100.000000
03-01-2018  100.808036  100.325886
04-01-2018  101.616560  102.307700

我发现上述链接中提出的问题与我的问题非常相似。我真的不明白为什么会出现attrError。

pandas
1个回答
2
投票

你的索引似乎是stringobject)dtype:

In [19]: df.index = pd.to_datetime(df.index).strftime('%d-%m-%Y')

In [20]: df
Out[20]:
                     A           B
02-01-2018  100.000000  100.000000
03-01-2018  100.808036  100.325886
04-01-2018  101.616560  102.307700
© www.soinside.com 2019 - 2024. All rights reserved.