adding new values to pandas df and increment timestamp

Question:

I have a time series dataset of a Pandas series df that I am trying to add a new value to the bottom of the df and then increment the timestamp which is the df index.

For example the new value I can add to the bottom of the df like this:

testday.loc[len(testday.index)] = testday_predict[0]

print(testday)

Which seems to work but the time stamp is just incremented:

    kW
Date    
2022-07-29 00:00:00 39.052800
2022-07-29 00:15:00 38.361600
2022-07-29 00:30:00 38.361600
2022-07-29 00:45:00 38.534400
2022-07-29 01:00:00 38.880000
... ...
2022-07-29 23:00:00 36.806400
2022-07-29 23:15:00 36.806400
2022-07-29 23:30:00 36.633600
2022-07-29 23:45:00 36.806400
96  44.482361    <---- my predicted value added at the bottom good except for the time stamp value of 96

Like the value of 96 is just the next value in the length of the df.index hopefully this makes sense.

If I try:

from datetime import timedelta

last_index_stamp = testday.last_valid_index()

print(last_index_stamp)

This returns:

Timestamp('2022-07-29 23:45:00')

And then I can add 15 minutes to this Timestamp (my data is 15 minute data) like this:

new_timestamp = last_index_stamp + timedelta(minutes=15)

print(new_timestamp)

Which returns what I am looking instead of the value of 96:

Timestamp('2022-07-30 00:00:00')

But how do I replace the value of 96 with new_timestampt? If I try:

testday.index[-1:] = new_timestamp

This will error out:

TypeError: Index does not support mutable operations

Any tips greatly appreciated…

Asked By: bbartling

||

Answers:

This should do the trick:

testday.loc[new_timestamp,:] = testday_predict[0]
Answered By: steam1901
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.