4

I have a DataFrame df whose index consist of Datetimes of each day in January for the years 1997 through 2011:

In [164]: df
Out[164]: 
             Tavg
1997-01-01  20.48
1997-01-02  37.49
...           ...
1997-01-31  37.49
1998-01-01  52.07
...           ...
2011-01-30  35.51
2011-01-31  29.03

From another DataFrame, I'd like to insert rows to df corresponding to Dec 31 of the previous year for each year; i.e., rows with index

In [166]: prev_dates = pd.date_range('1996-12-31', '2010-12-31', freq=pd.DateOffset(years=1))

In [167]: prev_dates
Out[167]: 
DatetimeIndex(['1996-12-31', '1997-12-31', '1998-12-31', '1999-12-31',
               '2000-12-31', '2001-12-31', '2002-12-31', '2003-12-31',
               '2004-12-31', '2005-12-31', '2006-12-31', '2007-12-31',
               '2008-12-31', '2009-12-31', '2010-12-31'],
              dtype='datetime64[ns]', freq='<DateOffset: kwds={'years': 1}>')

After inserting the values from these rows, I'd like the new df to look like

             Tavg
1996-12-31  <new value>
1997-01-01  20.48
1997-01-02  37.49
...           ...
1997-01-31  37.49
1997-12-31  <new value>
1998-01-01  52.07
...           ...
2011-01-30  35.51
2011-01-31  29.03

but I can't seem to find the right methods to achieve this.

1 Answer 1

4

reindex with union

df.reindex(prev_dates.union(df.index))

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I had no idea you could do this. Thanks.

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.