如何使用熊猫日期时间将数字转换为日期(通过使用这些数字作为与今天日期的距离)

Nab*_*zir 2 python date python-3.x pandas

如何将数字转换为日期(通过使用这些数字作为到今天的距离)。这是我的资料

    id  Number
    1   -3
    2   -1
    3    6
Run Code Online (Sandbox Code Playgroud)

因为今天是04/07/2019这样,它可以转换为

    id  Number  date
    1   -3      01/07/2019
    2   -1      03/07/2019
    3    6      10/07/2019

Run Code Online (Sandbox Code Playgroud)

ank*_*_91 6

您可以使用pd.to_timedelta()以下方法实现此目的:

df=df.assign(date=(pd.to_datetime('today')+pd.to_timedelta(df.Number,unit='D')).dt.date)
Run Code Online (Sandbox Code Playgroud)
   id  Number        date
0   1      -3  2019-07-01
1   2      -1  2019-07-03
2   3       6  2019-07-10
Run Code Online (Sandbox Code Playgroud)