使用条件将日期列添加到数据框中



我想在以下df:中添加4列日期


entry_date           sum        entry_date      payment_date                        
2000-01-05          8543304       2000-01-05    2000-02-18      
2000-01-06          370224        2000-01-06    2000-03-24  
2000-01-07          12040645      2000-01-07    2000-03-15      
2000-01-10          14633220      2000-01-10    2000-03-22      
2000-01-10          480648        2000-01-10    2000-03-21      
... ... ... ... ... ... ... ... ... ...
2020-12-29          1779688       2020-12-29    2021-01-25      
2020-12-30          596003        2020-12-30    2021-03-12      
2020-12-30          1412306       2020-12-30    2021-02-04      
2020-12-30          7588376       2020-12-30    2021-03-18      
2020-12-31          11229071      2020-12-31    2021-02-01      
5478 rows × 9 columns

我想创建4列日期:如果entry_date.day<13.

我已经编写了以下函数:

def get_15th_day_of_month(dt):
if dt.day < 13:
return dt.replace(day=15)
new_month = (dt.month + 1)
new_year = dt.year
while new_month > 12:
new_month = new_month - 12
new_year = new_year + 1
new_day = 15
return datetime(dt.year, new_month, new_day, calendar.monthrange(dt.year, new_month))

但由于缺乏经验,我不知道如何有效地实施。我写了一个只返回零的循环。如果15日不是工作日,也可以在下一个工作日进行抵消。

最终的数据帧(在我的梦中(应该如下所示:


entry_date      sum     entry_date      expiry_date  date1        date2       date3       date4 

2000-01-05     8543304    2000-01-05    2000-02-18    2000-02-15 2000-03-15  2000-04-15  2000-05-15
2000-01-06     370224     2000-01-06    2000-03-24    2000-01-15 2000-02-15  2000-03-15  2000-03-15
2000-01-07     12040645   2000-01-13    2000-03-15    2000-02-15 2000-03-15  2000-04-15  2000-05-15
2000-01-10     14633220   2000-01-10    2000-03-22      
2000-01-10     480648     2000-01-10    2000-03-21  
... ... ... ... ... ... ... ... ... ......  ... ... ... ... ... ... ... ... ... ... ... ... ... 
2020-12-29     1779688    2020-12-29    2021-01-25  
2020-12-30     596003     2020-12-30    2021-03-12  
2020-12-30     1412306    2020-12-30    2021-02-04  
2020-12-30     7588376    2020-12-30    2021-03-18  
2020-12-31     11229071   2020-12-31    2021-02-01  
5478 rows × 9 columns

您可以使用日期时间的月份周期,如果条件不匹配,则添加一个月,然后添加14 Days(因为每个月的第一天已经存在(:

df['entry_date'] = pd.to_datetime(df['entry_date'])
m = df['entry_date'].dt.day < 13
per = df['entry_date'].dt.to_period('m')
for i in range(4):
df[f'date{i + 1}'] = per.where(m, per + 1).add(i).dt.to_timestamp()+pd.Timedelta('14D')
print (df)
sum entry_date payment_date      date1      date2      date3  
2000-01-05   8543304 2000-01-05   2000-02-18 2000-01-15 2000-02-15 2000-03-15   
2000-01-06    370224 2000-01-06   2000-03-24 2000-01-15 2000-02-15 2000-03-15   
2000-01-07  12040645 2000-01-13   2000-03-15 2000-02-15 2000-03-15 2000-04-15   
2000-01-10  14633220 2000-01-10   2000-03-22 2000-01-15 2000-02-15 2000-03-15   
2000-01-10    480648 2000-01-10   2000-03-21 2000-01-15 2000-02-15 2000-03-15   
2020-12-29   1779688 2020-12-29   2021-01-25 2021-01-15 2021-02-15 2021-03-15   
2020-12-30    596003 2020-12-30   2021-03-12 2021-01-15 2021-02-15 2021-03-15   
2020-12-30   1412306 2020-12-30   2021-02-04 2021-01-15 2021-02-15 2021-03-15   
2020-12-30   7588376 2020-12-30   2021-03-18 2021-01-15 2021-02-15 2021-03-15   
2020-12-31  11229071 2020-12-31   2021-02-01 2021-01-15 2021-02-15 2021-03-15   
date4  
2000-01-05 2000-04-15  
2000-01-06 2000-04-15  
2000-01-07 2000-05-15  
2000-01-10 2000-04-15  
2000-01-10 2000-04-15  
2020-12-29 2021-04-15  
2020-12-30 2021-04-15  
2020-12-30 2021-04-15  
2020-12-30 2021-04-15  
2020-12-31 2021-04-15  

相关内容

  • 没有找到相关文章

最新更新