如何将熊猫系列的索引日期转换为DatetimeIndex?

时间:2020-10-07 16:19:31

标签: python pandas

以下数据来自熊猫系列,但我需要将日期转换为DatetimeIndex,格式如下:2020-08-17。该系列的索引应为pd.DatetimeIndex。有什么方法可以将其转换成这样?

8/17/20    14082780.0
8/18/20    14277100.0
8/19/20    14483216.0
8/20/20    14685442.0
8/21/20    14886403.0
Length: 212, dtype: float64

1 个答案:

答案 0 :(得分:1)

只需将索引更改为日期时间类型:

df.index = pd.to_datetime(df.index)

更一般地,对于非索引列:

df['Date']= pd.to_datetime(df['Date'])