Pandas 将行添加到日期时间索引的数据帧
我找不到解决这个问题的办法。为了模型预测的目的,我想将未来的日期添加到日期时间索引的数据帧中 这就是我现在的处境:Pandas 将行添加到日期时间索引的数据帧,pandas,dataframe,datetime,append,row,Pandas,Dataframe,Datetime,Append,Row,我找不到解决这个问题的办法。为了模型预测的目的,我想将未来的日期添加到日期时间索引的数据帧中 这就是我现在的处境: new_datetime = df2.index[-1:] # current end of datetime index increment = '1 days' # string for increment - eventually will be in a for loop to add add'l days new_datetime = new_datetime+pd.Ti
new_datetime = df2.index[-1:] # current end of datetime index
increment = '1 days' # string for increment - eventually will be in a for loop to add add'l days
new_datetime = new_datetime+pd.Timedelta(increment)
这就是我被困的地方。在线附加示例似乎总是显示带有
ignore_index=True
的示例,在我的例子中,我想使用正确的日期时间索引。假设您有以下df:
date value
0 2020-01-31 00:00:00 1
1 2020-02-01 00:00:00 2
2 2020-02-02 00:00:00 3
那么,增加未来天数的另一种选择是
df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods=6, freq='D', closed='right')}))
返回
date value
0 2020-01-31 00:00:00 1.0
1 2020-02-01 00:00:00 2.0
2 2020-02-02 00:00:00 3.0
0 2020-02-03 00:00:00 NaN
1 2020-02-04 00:00:00 NaN
2 2020-02-05 00:00:00 NaN
3 2020-02-06 00:00:00 NaN
4 2020-02-07 00:00:00 NaN
其中频率为D(天)天,周期为6天。假设您有此df:
date value
0 2020-01-31 00:00:00 1
1 2020-02-01 00:00:00 2
2 2020-02-02 00:00:00 3
那么,增加未来天数的另一种选择是
df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods=6, freq='D', closed='right')}))
返回
date value
0 2020-01-31 00:00:00 1.0
1 2020-02-01 00:00:00 2.0
2 2020-02-02 00:00:00 3.0
0 2020-02-03 00:00:00 NaN
1 2020-02-04 00:00:00 NaN
2 2020-02-05 00:00:00 NaN
3 2020-02-06 00:00:00 NaN
4 2020-02-07 00:00:00 NaN
其中频率为D(天)天,周期为6天。我认为这比必要的更加困难,因为我使用了日期时间索引而不是典型的整数索引。通过将“date”字段保留为常规列而不是索引来添加行是很简单的 我所做的一件事是添加一个reindex命令,这样我就不会得到不可靠的重复索引值:
df = df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods=21, freq='D', closed='right')}))
df = df.reset_index() # resets index
我想我让这变得比必要的更困难了,因为我使用了datetime索引而不是典型的整数索引。通过将“date”字段保留为常规列而不是索引来添加行是很简单的 我所做的一件事是添加一个reindex命令,这样我就不会得到不可靠的重复索引值:
df = df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods=21, freq='D', closed='right')}))
df = df.reset_index() # resets index
可能值得一看:可能值得一看:只要我使用datetime字段作为索引中的一个单独列,这就行了。我希望使用datetime索引格式作为我的预测索引。该示例可以工作,但对行使用整数而不是日期索引。只要我使用datetime字段作为索引中的单独列,就可以工作。我希望使用datetime索引格式作为我的预测索引。该示例有效,但对行使用整数而不是日期索引。