1

How do I add future dates to a data frame? This datetime delta only adds deltas to adjacent columns.

import pandas as pd
from datetime import timedelta
df = pd.DataFrame({
    'date': ['2001-02-01','2001-02-02','2001-02-03', '2001-02-04'],
    'Monthly Value': [100, 200, 300, 400]
})

df["future_date"] = df["date"] + timedelta(days=4)

print(df)

  date                future_date
0 2001-02-01 00:00:00 2001-02-05 00:00:00
1 2001-02-02 00:00:00 2001-02-06 00:00:00
2 2001-02-03 00:00:00 2001-02-07 00:00:00
3 2001-02-04 00:00:00 2001-02-08 00:00:00

Desired dataframe:

  date                future_date
0 2001-02-01 00:00:00 2001-02-01 00:00:00
1 2001-02-02 00:00:00 2001-02-02 00:00:00
2 2001-02-03 00:00:00 2001-02-03 00:00:00
3 2001-02-04 00:00:00 2001-02-04 00:00:00
4                     2001-02-05 00:00:00
5                     2001-02-06 00:00:00
6                     2001-02-07 00:00:00
7                     2001-02-08 00:00:00

3 Answers 3

3

You can do the following:

# set to timestamp
df['date'] = pd.to_datetime(df['date'])

# create a future date df
ftr =  (df['date'] + pd.Timedelta(4, unit='days')).to_frame()
ftr['Monthly Value'] = None

# join the future data
df1 = pd.concat([df, ftr], ignore_index=True)

        date Monthly Value
0 2001-02-01           100
1 2001-02-02           200
2 2001-02-03           300
3 2001-02-04           400
4 2001-02-05          None
5 2001-02-06          None
6 2001-02-07          None
7 2001-02-08          None
Sign up to request clarification or add additional context in comments.

Comments

2

I found that this also works:

df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods= 4, freq='d', closed='right')}))

Comments

1

If I understand you correctly,

we can create a new dataframe using the min of your date, and max + 4 days.

we just concat this back using axis = 1.

df['date'] = pd.to_datetime(df['date']) 

fdates = pd.DataFrame(
    pd.date_range(df["date"].min(), df["date"].max() + pd.DateOffset(days=4))
,columns=['future_date'])

df_new = pd.concat([df,fdates],axis=1)

print(df_new[['date','future_date','Monthly Value']])

0 2001-02-01  2001-02-01          100.0
1 2001-02-02  2001-02-02          200.0
2 2001-02-03  2001-02-03          300.0
3 2001-02-04  2001-02-04          400.0
4        NaT  2001-02-05            NaN
5        NaT  2001-02-06            NaN
6        NaT  2001-02-07            NaN
7        NaT  2001-02-08            NaN

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.