Insert dates in DataFrame rows

I have a DataFrame dfwhose index consists of Datetimes of each day in January for the period from 1997 to 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 would like to insert rows into the dfcorresponding December 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 pasting the values ​​from these lines, I would like the new one to dflook 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 not find the right methods to achieve this.

+4
source share
1 answer

reindex with union

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

+4
source

Source: https://habr.com/ru/post/1659227/


All Articles