简体   繁体   English

Python 比较列中的日期时间和 NaT

[英]Python Comparing Datetimes with NaT in Columns

I have a date frame like that has a submit and resolved date我有一个这样的日期框架,有一个提交和解决日期

  Incident ID   Submit_Date  Resolved_Date 
   INC001.       2021-02-25  2021-03-02     
   INC002        2021-02-27  2021-03-01    
   INC003        2021-02-27  NaT          
   INC004        2021-04-01  NaT  

I have a function to count the tickets if they meet a certain criteria but with the NaT's it won't read them with the comparison我有一个 function 来计算门票是否符合特定标准,但使用 NaT 时,它不会通过比较读取它们

part of the function I am having problems is like this The function it is looking if the rows resolved date is empty and the other tickets resolved date is empty then summing it for a count in a new column function 的一部分我遇到的问题是这样的 function 它正在查看行的解决日期是否为空,其他票的解决日期是否为空,然后将其相加以在新列中计数

 result = list()
  for row in df.iterrows():
     cur_data = row[1]
     result.append(((cur_data['Resolved_Date'] is pd.NaT) & (df['Resolved_Date'] is pd.NaT))).sum())

 df['Count'] = result  

I want the result to look like this我希望结果看起来像这样

  Incident ID Submit_Date  Resolved_Date  Count
   INC001        2021-02-25  2021-03-02     0
   INC002        2021-02-27  2021-03-01     0
   INC003        2021-02-27  NaT            2 # counting itself and the other NA resolved date
   INC004        2021-04-01  NaT            2 # counting itself and the other NA resolved date

Right now it is ignoring the NaTs现在它忽略了 NaT

If you are looking to get total count of NaT in Resolved_Date column and put it in a new column against NaT values of Resolved_Date then this should work.如果您希望在Resolved_Date列中获取NaT的总数并将其放在一个新列中,以针对Resolved_DateNaT值,那么这应该可以工作。

Code代码

NaT_check = df['Resolved_Date'].isna()
df['Count'] = np.where(NaT_check, NaT_check.sum(),0)
df

Output Output

    Incident ID Submit_Date Resolved_Date   Count
0   INC001  2021-02-25  2021-03-02          0
1   INC002  2021-02-27  2021-03-01          0
2   INC003  2021-02-27  NaT                 2
3   INC004  2021-04-01  NaT                 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM