获取 pandas 中一个月级别的两个日期时间之间的天数

get number of days between two datetimes on a month level in pandas

这是一个数据集,其中包含人员被分配到某个角色的时间,以及他们的开始日期,以及他们担任该角色的月份的年月:

  | ID | Name | strt_dt | end_dt | yearmo | 
  | 1  | Jay  | 4-22-19 | 7-30-19| 201904 | 
  | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |  
  | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |   
  | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |  
  | 2  | Fao  | 7-14-19 |10-14-19| 201907 |    
  | 2  | Fao  | 7-14-19 |10-14-19| 201908 |   
  | 2  | Fao  | 7-14-19 |10-14-19| 201909 |   
  | 2  | Fao  | 7-14-19 |10-14-19| 201910 |    

我要计算这个人在这个角色中的每一年,那个月有多少天他们在这个角色中。输出应如下所示:

  | ID | Name | strt_dt | end_dt | yearmo | no_of days|
  | 1  | Jay  | 4-22-19 | 7-30-19| 201904 |  9 |
  | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |  31|  
  | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |  30|  
  | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |  30| 
  | 2  | Fao  | 7-14-19 |10-14-19| 201907 |  18|  
  | 2  | Fao  | 7-14-19 |10-14-19| 201908 |  31|  
  | 2  | Fao  | 7-14-19 |10-14-19| 201909 |  30|  
  | 2  | Fao  | 7-14-19 |10-14-19| 201910 |  14|  

我试图从 strt 中提取它们的日期(将其减去 30 以获得 ddays 的编号)和结束日期并创建一个单独的列。但我坚持如何从那里开始。欢迎任何想法或建议。

df['strt_yearmo'] = df['strt_dt'].dt.year * 100 +df['strt_dt'].dt.month
df['end_yearmo'] = df['end_dt'].dt.year * 100 +df['end_dt'].dt.month


  | ID | Name | strt_dt | end_dt | yearmo | strt_yearmo|end_yearmo|
  | 1  | Jay  | 4-22-19 | 7-30-19| 201904 |  201904    |201907|
  | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |  201904    |201907|
  | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |  201904    |201907|  
  | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |  201904    |201907 |
  | 2  | Fao  | 7-14-19 |10-14-19| 201907 |  201907    |201910 |
  | 2  | Fao  | 7-14-19 |10-14-19| 201908 |  201907    |201910 | 
  | 2  | Fao  | 7-14-19 |10-14-19| 201909 |  201907    |201910 |
  | 2  | Fao  | 7-14-19 |10-14-19| 201910 |  201907    |201910 | 

在将日期强制转换为日期时间并在 yearmo

中提取结束月份日期后使用 np.select(condition, choice,alternative)

从 yearmo 中提取月末日期

df['startmo']=pd.to_datetime(df['yearmo'].astype(str), format='%Y%m')+ pd.offsets.MonthEnd(0)

强迫strt_dtend_dt约会

datedf['strt_dt'],df['end_dt']=pd.to_datetime(df['strt_dt']),pd.to_datetime(df['end_dt'])

拿出条件

conditions=[df.startmo.dt.month==df.strt_dt.dt.month, df.startmo.dt.month==df.end_dt.dt.month]

#If month in yearmo is the same with strt_dt,substract strt_dt from endmont.
#If month in yearmo is the same with end_dt, extract the days in end_dt

针对上述每个条件提出相应的选择

choices=[df.startmo.sub(df.strt_dt).dt.days+1,df.end_dt.dt.day]

通过匹配条件和选择来计算天数。也包括备选方案。这里的替代方案是开始和结束的月份与 yearmo 不匹配,这意味着月份在中间所以只需提取天数作为条件的替代方案

df['no_of days']=np.select(conditions,choices,df.startmo.dt.day)




ID Name    strt_dt     end_dt  yearmo    startmo  no_of days
0   1  Jay 2019-04-22 2019-07-30  201904 2019-04-30           9
1   1  Jay 2019-04-22 2019-07-30  201905 2019-05-31          31
2   1  Jay 2019-04-22 2019-07-30  201906 2019-06-30          30
3   1  Jay 2019-04-22 2019-07-30  201907 2019-07-31          30
4   2  Fao 2019-07-14 2019-10-14  201907 2019-07-31          18
5   2  Fao 2019-07-14 2019-10-14  201908 2019-08-31          31
6   2  Fao 2019-07-14 2019-10-14  201909 2019-09-30          30
7   2  Fao 2019-07-14 2019-10-14  201910 2019-10-31          14