python – Numpy: Checking if a value is NaT

python – Numpy: Checking if a value is NaT

can check for NaT with pandas.isnull:

>>> import numpy as np
>>> import pandas as pd
>>> pd.isnull(np.datetime64(NaT))
True

If you dont want to use pandas you can also define your own function (parts are taken from the pandas source):

nat_as_integer = np.datetime64(NAT).view(i8)

def isnat(your_datetime):
    dtype_string = str(your_datetime.dtype)
    if datetime64 in dtype_string or timedelta64 in dtype_string:
        return your_datetime.view(i8) == nat_as_integer
    return False  # it cant be a NaT if its not a dateime

This correctly identifies NaT values:

>>> isnat(np.datetime64(NAT))
True

>>> isnat(np.timedelta64(NAT))
True

And realizes if its not a datetime or timedelta:

>>> isnat(np.timedelta64(NAT).view(i8))
False

In the future there might be an isnat-function in the numpy code, at least they have a (currently open) pull request about it: Link to the PR (NumPy github)

Since NumPy version 1.13 it contains an isnat function:

>>> import numpy as np
>>> np.isnat(np.datetime64(nat))
True

It also works for arrays:

>>> np.isnat(np.array([nat, 1, 2, 3, 4, nat, 5], dtype=datetime64[D]))
array([ True, False, False, False, False,  True, False], dtype=bool)

python – Numpy: Checking if a value is NaT

INTRO: This answer was written in a time when Numpy was version 1.11 and behaviour of NAT comparison was supposed to change since version 1.12. Clearly that wasnt the case and the second part of answer became wrong. The first part of answer may be not applicable for new versions of numpy. Be sure youve checked MSeiferts answers below.


When you make a comparison at the first time, you always have a warning. But meanwhile returned result of comparison is correct:

import numpy as np    
nat = np.datetime64(NaT)

def nat_check(nat):
    return nat == np.datetime64(NaT)    

nat_check(nat)
Out[4]: FutureWarning: In the future, NAT == x and x == NAT will always be False.
True

nat_check(nat)
Out[5]: True

If you want to suppress the warning you can use the catch_warnings context manager:

import numpy as np
import warnings

nat = np.datetime64(NaT)

def nat_check(nat):
    with warnings.catch_warnings():
        warnings.simplefilter(ignore)
        return nat == np.datetime64(NaT)    

nat_check(nat)
Out[5]: True


EDIT: For some reason behavior of NAT comparison in Numpy version 1.12 wasnt change, so the next code turned out to be inconsistent.

And finally you might check numpy version to handle changed behavior since version 1.12.0:

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


EDIT: As MSeifert mentioned, Numpy contains isnat function since version 1.13.

Leave a Reply

Your email address will not be published. Required fields are marked *