0

I am pretty new to Datetime and pandas. I want to add new date row based on the given value. If the input is 2 then the program should two dates from the last row value. I could able to get the dates but I Am not able to insert them into the data frame. Here the Date is an index. Is there any way to do it?

My input:

df=
            column1
2020-12-22    1
2020-12-23    2
2020-12-24    3
start = df.index.max()
numdays =4
date_list = [start - datetime.timedelta(days=x) for x in range(numdays)]
date_list

The output is as follows:

[Timestamp('2020-12-24 00:00:00'),
 Timestamp('2020-12-25 00:00:00'),
 Timestamp('2020-12-26 00:00:00'),
 Timestamp('2020-12-27 00:00:00')]

Expected Output:

    column1
2020-12-22    1
2020-12-23    2
2020-12-24    3
2020-12-25    Naan
2020-12-26    Naan
2020-12-27    Naan

1 Answer 1

1

A way to do so :

import datetime as dt
delta = int(input('Enter the number of row'))
for i in range(delta):
    df.loc[df.index[-1]+dt.timedelta(days=1)] = np.nan
df

OUTPUT:

enter image description here

As I didn't understand if you want a real input, I've coded it, but if not, you just have to assign your value to ‘delta‘ without asking via input

import datetime as dt
delta = 2
for i in range(delta):
    df.loc[df.index[-1]+dt.timedelta(days=1)] = np.nan
df
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.