Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将日期插入数据框的行中_Python_Datetime_Pandas_Dataframe - Fatal编程技术网

Python 将日期插入数据框的行中

Python 将日期插入数据框的行中,python,datetime,pandas,dataframe,Python,Datetime,Pandas,Dataframe,我有一个数据框df,其索引由1997年至2011年1月每天的日期时间组成: 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 从另一个数据帧中,我想为每一年插入对应于上一年12

我有一个数据框df,其索引由1997年至2011年1月每天的日期时间组成:

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
从另一个数据帧中,我想为每一年插入对应于上一年12月31日的
df
行;i、 例如,带有索引的行

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}>')

但是我似乎找不到正确的方法来实现这一点。

reindex
with
union

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

我不知道你能做到这一点。谢谢
df.reindex(prev_dates.union(df.index))