Pandas:计算日期列之间不包括周末的天数

问题描述 投票:0回答:0

开始的数据框:

''' df = pd.DataFrame({

# some ways to create random data
'User Story Id':np.random.choice( ['US111111','US111112','US111113','US222221','US222222','US222223'], 6,replace=False),
'Feature Id':np.random.choice( ['F999999','F888888'], 6),
'Sprint Label':np.random.choice( ['ABC 23.1.1'], 6),
'Team name':np.random.choice( ['panda','python','shark'], 6),


# a date range and set of random dates
'story_being_groomed_ts':pd.date_range('1/10/2023  8:07:21 AM', periods=6, freq='D'),
'story_in_progress_ts':np.random.choice( pd.date_range('1/10/2023  8:07:21 AM', periods=10,
                      freq='D'), 6, replace=False),
'story_complete_ts':np.random.choice( pd.date_range('1/20/2023  8:07:21 AM', periods=10,
                      freq='D'), 6, replace=False),
'story_accepted_ts':np.random.choice( pd.date_range('1/30/2023  8:07:21 AM', periods=10,
                      freq='D'), 6, replace=False),
'story_release_to_prod_ts':np.random.choice( pd.date_range('2/10/2023  8:07:21 AM', periods=10,
                      freq='D'), 6, replace=False)
})

'''

我想添加三个额外的列:

'Lead_time_1':计算'story_in_progress_ts'和'story_being_groomed_ts'之间的工作日

'Lead_time_2':计算'story_accepted_ts'和'story_in_progress_ts'之间的工作日

'Lead_time_3':计算'story_release_to_prod_ts'和'story_accepted_ts'之间的工作日

每个用户故事 ID 都有一组日期时间戳列。 对于每个用户故事 ID,我想如上所述计算提前期:

  • 从交货时间计算中排除周末
  • 如果对于 Lead_time_3 计算,'story_release_to_prod_ts' 为空(意味着故事被接受但未发布到生产环境),那么 Lead_time_3 计算应该为'空'
  • 如果对于 Lead_time_2 计算,“story_accepted_ts”为空(意味着故事正在进行中但未被产品所有者接受),那么 Lead_time_2 计算应该为“空”
  • 如果对于 Lead_time_1 计算,“story_in_progress_ts”为空白(意味着故事正在整理中,但开发人员并未进行),那么 Lead_time_1 计算应该为“空白”
  • Lead_time_1 | Lead_time_2 |如果故事在“story_in_progress_ts”列下没有有效的日期时间戳,Lead_time_3 计算应该为空。

我的预期结果:

'''

User Story Id | Feature Id | Sprint Label | Team name | story_being_groomed_ts | story_in_progress_ts | story_complete_ts | story_accepted_ts | story_release_to_prod_ts | Lead Time 1 |Lead Time 2 | Lead Time 3 |

US111112 | F999999 |美国广播公司 23.1.1 |鲨鱼 | 2023-01-10 08:07:21 | 2023-01-11 08:07:21 | 2023-01-20 08:07:21 | 2023-02-04 08:07:21 | 2023-02-17 08:07:21 | 1 | 1 | 1

'''

我尝试使用以下内容构建日期时间差异列。 df[['story_being_groomed_ts','story_in_progress_ts']] = df[['story_being_groomed_ts','story_in_progress_ts']].apply(pd.to_datetime) #if 需要转换 df['准备时间'] = (df['story_in_progress_ts'] - df['story_being_groomed_ts']).dt.days

python pandas date-difference
© www.soinside.com 2019 - 2024. All rights reserved.