Reputation: 61
I have referred previous links in stackoverflow but still not getting the exact answer. I am using Colab notebook. My df.tail() looks like this:
df.tail()
I want to create next empty 24 rows for 24 months for prediction My code:
from pandas.tseries.offsets import DateOffset
future_dates = [df.index[-1] + DateOffset(months = x) for x in range (0,24)]
It gives me error:
TypeError: unsupported operand type(s) for +: 'int' and 'DateOffset'.
Upvotes: 0
Views: 2423
Reputation: 77337
df.index
is the indexer for the dataframe, not the dataframe itself. The default indexer for a dataframe is a pandas RangeIndex
object. When you index this object, it returns the integer index that your index resolves to. Negative indexes count from the end, so [-1]
is the final valid index of the dataframe. For a dataframe of 509 rows, 508 is the last valid index.
Instead, you can use the iloc
method to get rows by integer index. As with everything pandas, iloc has its complications (see the docs) but you can do
future_dates = [df.iloc[-1]["Date"] + DateOffset(months = x) for x in range (0,24)]
Upvotes: 1