python - Numpy: Checking if a value is NaT -


nat = np.datetime64('nat') nat == nat >> futurewarning: in future, 'nat == x' , 'x == nat' false.  np.isnan(nat) >> typeerror: ufunc 'isnan' not supported input types, , inputs not safely coerced supported types according casting rule ''safe'' 

how can check if datetime64 nat? can't seem dig out of docs. know pandas can it, i'd rather not add dependency basic.

when make comparison @ first time, have warning. meanwhile returned result of comparison correct:

import numpy np     nat = np.datetime64('nat')  def nat_check(nat):     return nat == np.datetime64('nat')      nat_check(nat) out[4]: futurewarning: in future, 'nat == x' , 'x == nat' false. true  nat_check(nat) out[5]: true 

if want suppress warning can use catch_warnings context manager:

import numpy np import warnings  nat = np.datetime64('nat')  def nat_check(nat):     warnings.catch_warnings():         warnings.simplefilter("ignore")         return nat == np.datetime64('nat')      nat_check(nat) out[5]: true 

and might check numpy version handle changed behavior since version 1.12.0:

def nat_check(nat):     if [int(x) x in np.__version__.split('.')[:-1]] > [1, 11]:         return nat != nat     warnings.catch_warnings():         warnings.simplefilter("ignore")         return nat == np.datetime64('nat') 


edit: mseifert mentioned, numpy contains isnat function since version 1.13.


Comments