pandas - Extend Index of a DataFrame setting all columns for new rows to NaN?

前端 未结 6 458
日久生厌
日久生厌 2021-02-05 03:58

I have time-indexed data:

df2 = pd.DataFrame({ \'day\': pd.Series([date(2012, 1, 1), date(2012, 1, 3)]), \'b\' : pd.Series([0.22, 0.3]) })
df2 = df2.set_index(\'         


        
6条回答
  •  粉色の甜心
    2021-02-05 04:24

    def extendframe(df, ndays):
        """
        (df, ndays) -> df that is padded by ndays in beginning and end
        """
        ixd = df.index - datetime.timedelta(ndays)
        ixu = df.index + datetime.timedelta(ndays)
        ixx = df.index.union(ixd.union(ixu))
        df_ = df.reindex(ixx)
        return df_
    

提交回复
热议问题