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

前端 未结 6 457
日久生厌
日久生厌 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:05

    Not exactly the question since here you know that the second index is all days in January, but suppose you have another index say from another data frame df1, which might be disjoint and with a random frequency. Then you can do this:

    ix = pd.DatetimeIndex(list(df2.index) + list(df1.index)).unique().sort_values()
    df2.reindex(ix)
    

    Converting indices to lists allows one to create a longer list in a natural way.

提交回复
热议问题