我的数据框如下:
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
我想追加2个连续日期并获得以下数据框。
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
new = pd.date_range(dates.Dates.iloc[-1], periods=2)
print (new)
答案 0 :(得分:1)
使用append
,但必须通过索引[1:]
来删除第一个新值:
new = pd.date_range(df.Dates.iloc[-1], periods=3)
s = pd.Series(new[1:])
new = df['Dates'].append(s, ignore_index=True)
print (new)
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
dtype: datetime64[ns]
new = pd.date_range(df.Dates.iloc[-1], periods=3)
df1 = pd.DataFrame(new[1:], columns=['Dates'])
df = df.append(df1, ignore_index=True)
print (df)
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
或重新构建DataFrame
:
new = pd.date_range(df.Dates.iloc[0], periods=len(df) + 2)
df1 = pd.DataFrame(new, columns=['Dates'])
print (df1)
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06