无法比较日期

huangapple go评论61阅读模式
英文:

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 = &quot;2023-02-08&quot;
date2 = &quot;2023-05-23&quot;

newdate1 = time.strptime(date1, &quot;%Y-%m-%d&quot;)
newdate2 = time.strptime(date2, &quot;%Y-%m-%d&quot;)

df[&#39;StateChangeDate&#39;] = pd.to_datetime(df[&#39;StateChangeDate&#39;], format=&#39;%Y-%m-%d&#39;)
df[&#39;identifier&#39;] = df.apply(lambda row: 1 if (&#39;Test Case&#39; in row[&#39;WorkItemType&#39;]) and (&#39;Ready&#39; in row[&#39;State&#39;])  and (newdate1 &lt;= row[&#39;StateChangeDate&#39;] &lt;= newdate2) else 0, axis=1)

filtered_df = df[df[&#39;identifier&#39;] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df[&#39;identifier&#39;]

method2:

df[&#39;identifier&#39;] = df.apply(lambda row: 1 if (&#39;Test Case&#39; in row[&#39;WorkItemType&#39;]) and (&#39;Ready&#39; in row[&#39;State&#39;])  and (&#39;2023-03-22&#39; &lt;= row[&#39;StateChangeDate&#39;] &lt;= &#39;2023-04-04&#39;) else 0, axis=1)
filtered_df = df[df[&#39;identifier&#39;] != 0]
filtered_df = filtered_df.reset_index(drop=True)
y1.append(filtered_df.shape[0])
del filtered_df[&#39;identifier&#39;]

答案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[&#39;WorkItemType&#39;].str.contains(&#39;Test Case&#39;)
     &amp; df[&#39;State&#39;].str.contains(&#39;Ready&#39;)
     &amp; df[&#39;StateChangeDate&#39;].gt(date1)
     &amp; df[&#39;StateChangeDate&#39;].lt(date2))

filtered_df = df[m]

huangapple
  • 本文由 发表于 2023年5月22日 17:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定