如何根据python中的另一列自动添加日期?

How to add date automatically based on the other column in python?

我对 Datetime 和 pandas 还很陌生。因此,我创建了一个代码来为列生成新数据(额外行),而我的索引是日期。现在根据在列中创建的新行,日期也应该根据上面的内容自动添加。有什么办法吗?

我的输入:

df=
            column1
2020-12-22    1
2020-12-23    2
2020-12-24    3

我运行在column1中获取更多数据的代码后,输出如下:

            column1
2020-12-22    1
2020-12-23    2
2020-12-24    3
0             4
1             5
3             6
4             7

预期输出:

    column1
2020-12-22    1
2020-12-23    2
2020-12-24    3
2020-12-25    4
2020-12-26    5
2020-12-27    6
2020-12-28    7

您可以通过 to_datetime 的原始 DatetimeIndex 按最大日期时间创建 DatetimeIndex:

max1 = df.index.max()
df1.index = pd.to_datetime(df1.index + 1, origin=max1, unit='D')