Python:如何更改设置为索引的pandas中的日期

时间:2017-06-05 13:26:12

标签: python pandas datetime

我需要将最后一个日期设置为今天的日期。示例:2016-05-18至2017-06-05。 但是当我执行df.index[-1] = today时,它会返回此错误 TypeError: Index does not support mutable operations

>>> today
0   2017-06-05
Name: trading_day, dtype: datetime64[ns]


           Stock      Open      High       Low     Close Adj Close  Volume
Date                                                                      
2016-05-13   AAD  5.230000  5.260000  5.200000  5.260000  5.260000    5000
2016-05-16   AAD  5.220000  5.260000  5.220000  5.260000  5.260000    6000
2016-05-17   AAD  5.210000  5.260000  5.210000  5.260000  5.260000    2000
2016-05-18   AAD  5.200000  5.250000  5.200000  5.250000  5.250000    3000  

>>> df.index[-1] = today
TypeError: Index does not support mutable operations

我需要的是

           Stock      Open      High       Low     Close Adj Close  Volume
Date                                                                      
2016-05-13   AAD  5.230000  5.260000  5.200000  5.260000  5.260000    5000
2016-05-16   AAD  5.220000  5.260000  5.220000  5.260000  5.260000    6000
2016-05-17   AAD  5.210000  5.260000  5.210000  5.260000  5.260000    2000
2017-06-05   AAD  5.200000  5.250000  5.200000  5.250000  5.250000    3000

只有最后的日期才会改变。

2 个答案:

答案 0 :(得分:1)

您可以使用重命名

df.rename({df.index[-1]: 'today'}, inplace = True)

你得到了

        Stock   Open    High    Low Close   Adj Close.1 Volume
Date                                
2016-05-13  AAD     5.23    5.26    5.20    5.26    5.26    5000
2016-05-16  AAD     5.22    5.26    5.22    5.26    5.26    6000
2016-05-17  AAD     5.21    5.26    5.21    5.26    5.26    2000
today       AAD     5.20    5.25    5.20    5.25    5.25    3000

将代码更改为

import datetime as dt    
df.rename({df.index[-1]: dt.date.today()}, inplace = True)

你得到了

    Stock   Open    High    Low Close   Adj Close.1 Volume
Date                                
2016-05-13  AAD     5.23    5.26    5.20    5.26    5.26    5000
2016-05-16  AAD     5.22    5.26    5.22    5.26    5.26    6000
2016-05-17  AAD     5.21    5.26    5.21    5.26    5.26    2000
2016-06-05  AAD     5.20    5.25    5.20    5.25    5.25    3000

答案 1 :(得分:0)

从df1开始:

df1.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 4 entries, 2016-05-13 to 2016-05-18
Data columns (total 7 columns):
Stock        4 non-null object
Open         4 non-null float64
High         4 non-null float64
Low          4 non-null float64
Close        4 non-null float64
Adj Close    4 non-null float64
Volume       4 non-null int64
dtypes: float64(5), int64(1), object(1)
memory usage: 256.0+ bytes

print(df1)

           Stock  Open  High   Low  Close  Adj Close  Volume
2016-05-13   AAD  5.23  5.26  5.20   5.26       5.26    5000
2016-05-16   AAD  5.22  5.26  5.22   5.26       5.26    6000
2016-05-17   AAD  5.21  5.26  5.21   5.26       5.26    2000
2016-05-18   AAD  5.20  5.25  5.20   5.25       5.25    3000

今天似乎是一个具有一行数据类型datetime的系列。

today
Out[36]:
0   2017-06-05
dtype: datetime64[ns]

让我们用今天的第一行替换最后一个索引:

df2 = df1.rename({df1.index[-1]: today.iloc[0]})
print(df2)

           Stock  Open  High   Low  Close  Adj Close  Volume
2016-05-13   AAD  5.23  5.26  5.20   5.26       5.26    5000
2016-05-16   AAD  5.22  5.26  5.22   5.26       5.26    6000
2016-05-17   AAD  5.21  5.26  5.21   5.26       5.26    2000
2017-06-05   AAD  5.20  5.25  5.20   5.25       5.25    3000