Numpy: Checking if a value is NaT

前端 未结 6 1652
余生分开走
余生分开走 2020-12-23 21:12
nat = np.datetime64(\'NaT\')
nat == nat
>> FutureWarning: In the future, \'NAT == x\' and \'x == NAT\' will always be False.

np.isnan(nat)
>> TypeError:         


        
6条回答
  •  离开以前
    2020-12-23 21:46

    Very simple and surprisingly fast: (without numpy or pandas)

        str( myDate ) == 'NaT'            # True if myDate is NaT
    

    Ok, it's a little nasty, but given the ambiguity surrounding 'NaT' it does the job nicely.

    It's also useful when comparing two dates either of which might be NaT as follows:

       str( date1 ) == str( date1 )       # True
       str( date1 ) == str( NaT )         # False
       str( NaT )   == str( date1 )       # False
    
    wait for it...
    
       str( NaT )   == str( Nat )         # True    (hooray!)
    

提交回复
热议问题