我有一个带有日期和数字的数据框(df)。我想在日期上加上数字。如何使用pd.offsets()将df ['additional_days']系列添加到df ['start_date']系列中?有更好的方法吗?
开始日期后的第二天
2018-03-29 360
2018-07-31 0
2018-11-01 360
2016-11-03 720
2018-12-04 480
尝试时出现错误
lvls <- stringr::str_sort(unique(df$x), numeric = TRUE)
df$x <- factor(df$x, levels = lvls)
ggplot(data = df, aes(x = x, y = value,
group = variable, color = variable)) +
geom_line()
这是错误
df['start_date'] + pd.offsets.Day(df['additional_days'])
答案 0 :(得分:2)
使用pd.to_timedelta
import pandas as pd
#df['start_date'] = pd.to_datetime(df.start_date)
df['start_date'] + pd.to_timedelta(df.additional_days, unit='d')
#0 2019-03-24
#1 2018-07-31
#2 2019-10-27
#3 2018-10-24
#4 2020-03-28
#dtype: datetime64[ns]