英文:
Unable to compare dates
问题
I'm trying to compare the StateChangeDate field having the condition that it should be between the two dates but it's returning 0 as output. Please help to get the output.
method1:
date1 = "2023-02-08"
date2 = "2023-05-23"
newdate1 = time.strptime(date1, "%Y-%m-%d")
newdate2 = time.strptime(date2, "%Y-%m-%d")
df['StateChangeDate'] = pd.to_datetime(df['StateChangeDate'], format='%Y-%m-%d')
df['identifier'] = df.apply(lambda row: 1 if ('Test Case' in row['WorkItemType']) and ('Ready' in row['State']) and (newdate1 <= row['StateChangeDate'] <= newdate2) else 0, axis=1)
filtered_df = df[df['identifier'] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df['identifier']
method2:
df['identifier'] = df.apply(lambda row: 1 if ('Test Case' in row['WorkItemType']) and ('Ready' in row['State']) and ('2023-03-22' <= row['StateChangeDate'] <= '2023-04-04') else 0, axis=1)
filtered_df = df[df['identifier'] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df['identifier']
英文:
I'm trying to compare the StateChangeDate field having the condition that it should be between the two dates but it's returning 0 as output. Please help to get the output.
method1:
date1 = "2023-02-08"
date2 = "2023-05-23"
newdate1 = time.strptime(date1, "%Y-%m-%d")
newdate2 = time.strptime(date2, "%Y-%m-%d")
df['StateChangeDate'] = pd.to_datetime(df['StateChangeDate'], format='%Y-%m-%d')
df['identifier'] = df.apply(lambda row: 1 if ('Test Case' in row['WorkItemType']) and ('Ready' in row['State']) and (newdate1 <= row['StateChangeDate'] <= newdate2) else 0, axis=1)
filtered_df = df[df['identifier'] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df['identifier']
method2:
df['identifier'] = df.apply(lambda row: 1 if ('Test Case' in row['WorkItemType']) and ('Ready' in row['State']) and ('2023-03-22' <= row['StateChangeDate'] <= '2023-04-04') else 0, axis=1)
filtered_df = df[df['identifier'] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df['identifier']
答案1
得分: 1
你可以使用布尔索引:
m = (df['WorkItemType'].str.contains('测试用例')
& df['State'].str.contains('已准备')
& df['StateChangeDate'].gt(date1)
& df['StateChangeDate'].lt(date2))
filtered_df = df[m]
英文:
You can use boolean indexing:
m = (df['WorkItemType'].str.contains('Test Case')
& df['State'].str.contains('Ready')
& df['StateChangeDate'].gt(date1)
& df['StateChangeDate'].lt(date2))
filtered_df = df[m]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论