1

How do I create a new dataframe date column based upon where another dataframe column date left off?

df1 = pd.DataFrame({
        'date': ['2001-01-01','2001-01-02','2001-01-03', '2001-01-04', '2001-01-05'],
        'prod': [800, 900, 1200, 700, 600]})

last_date = max(df1.date)

df2 = pd.DataFrame({'value': [9,1,18,4,5]})

Desired dataframe:

print(df2)

   value        date
0      9  2001-01-06
1      1  2001-01-07
2     18  2001-01-08
3      4  2001-01-09
4      5  2001-01-10

I've tried df2['date'] = pd.date_range('2001-01-06', periods=20, freq='days'), however this extends the length of df2.

1
  • periods=len(df2)? Commented Jan 22, 2020 at 16:26

1 Answer 1

2

Use pd.date_range after converting the df1['date'] column to datetime:

df1['date'] = pd.to_datetime(df1['date'])
df2['date'] = pd.date_range(df1['date'].max()+pd.Timedelta(1,unit='d'),periods=len(df2))

print(df2)

   value       date
0      9 2001-01-06
1      1 2001-01-07
2     18 2001-01-08
3      4 2001-01-09
4      5 2001-01-10
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.