user7983871
user7983871

Reputation:

Python: How to change a date in pandas which set as index

I need to set the last date to today date. Example: 2016-05-18 to 2017-06-05. However when I do df.index[-1] = today, it return this error 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

What I needed is

           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

Only the last date is change.

Upvotes: 1

Views: 1008

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Start with 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

today appears to be a series with one row datatype datetime.

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

Let's replace last index with first row of today:

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

Upvotes: 0

Vaishali
Vaishali

Reputation: 38415

You can use rename

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

You get

        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

Change the code to

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

And you get

    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

Upvotes: 2

Related Questions