在熊猫中操作日期

时间:2019-04-25 10:01:15

标签: python pandas

由于我是R背景,所以我试图理解Python中的各种功能。

我面临的问题是:如何根据条件从熊猫中添加和减去天/年/月?在R中,我可以使用dplyr程序包,其中mutateifelse将允许我与lubridate程序包一起实现它。

这是我在R中的可复制数据:

df = data.frame(date1=c("2017-07-07", "2017-02-11", "2017-05-22", "2017-04-27")) 

library(lubridate)
df$date1 <- ymd(df$date1) + years(2)
df$day <- wday(df$date1, label=TRUE)

输入

       date1 day
1 2019-07-07 Sun
2 2019-02-11 Mon
3 2019-05-22 Wed
4 2019-04-27 Sat
  

任务:如果日期为“ Sun”,则在日期中添加年份,如果日期为“ Sat”,则从日期中减去年份,否则为IGNORE

R代码

library(dplyr)

df %>% mutate(newdate = ifelse(df$day == "Sun", date1 %m+% years(1), 
                               ifelse(df$day == "Sat", date1 %m-% years(1), date1))) -> df

df$newdate <- as.Date(df$newdate, origin = "1970-01-01")
df$newday <- wday(df$newdate, label=T)
df

输出

       date1 day    newdate newday
1 2019-07-07 Sun 2020-07-07    Tue
2 2019-02-11 Mon 2019-02-11    Mon
3 2019-05-22 Wed 2019-05-22    Wed
4 2019-04-27 Sat 2018-04-27    Fri

有人可以和我分享如何使用Pandas实现此输出吗?

2 个答案:

答案 0 :(得分:2)

使用DateOffset添加年份,并添加Series.dt.strftime%a作为日期名称:

df = pd.DataFrame({'date1':pd.to_datetime(["2017-07-07", 
                                           "2017-02-11", 
                                           "2017-05-22", 
                                           "2017-04-27"])}) 

df['date1'] += pd.offsets.DateOffset(years=2)
df['day'] = df['date1'].dt.strftime('%a')

对于使用多个布尔掩码的设置值,请使用numpy.select

masks = [df['day'] == 'Sun', 
         df['day'] == 'Sat']
vals = [df['date1'] + pd.offsets.DateOffset(years=1),
        df['date1'] - pd.offsets.DateOffset(years=1)]

df['newdate'] = np.select(masks, vals, default=df['date1'])
df['newday'] = df['newdate'].dt.strftime('%a')

print (df)
       date1  day    newdate newday
0 2019-07-07  Sun 2020-07-07    Tue
1 2019-02-11  Mon 2019-02-11    Mon
2 2019-05-22  Wed 2019-05-22    Wed
3 2019-04-27  Sat 2018-04-27    Fri

答案 1 :(得分:1)

这对您应该很好:

df = pd.DataFrame(data = {'date1':["2017-07-07", "2017-02-11", "2017-05-22", "2017-04-27"], 'day':["Sun", "Mon", "Wed", "Sat"]})


df['date1']= pd.to_datetime(df['date1'])
df['date1'] = df['date1'] + pd.DateOffset(years=2)

def func_year(row):
if row['day'] == 'Sun':
    date = row['date1'] +  pd.DateOffset(years=1)
elif row['day'] == 'Sat':
    date = row['date1'] -  pd.DateOffset(years=1)
else:
    date = row['date1']
return date

df['new_date'] = df.apply(func_year, axis=1)