NumPy or Pandas: How to check a Value or an Array for NaT

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. NumPy: How to check a Value or an Array for NaT
  2. Checking a Value or an Array for NaT with pandas.isnull()
  3. Checking a value for NaT by converting it to a string

# NumPy: How to check a Value or an Array for NaT

You can use the numpy.isnat() method to check a value or an array for NaT.

The isnat() method returns True if the value is NaT and False otherwise.

main.py
import numpy as np not_a_time = np.datetime64('NaT') print(not_a_time) # ๐Ÿ‘‰๏ธ NaT print('-' * 50) if np.isnat(not_a_time): # ๐Ÿ‘‡๏ธ This runs print('The value has a type of NaT') else: print('The value does NOT have a type of NaT') print('-' * 50) dt = np.datetime64('2024-09-24') print(np.isnat(dt)) # ๐Ÿ‘‰๏ธ False

check for nat in numpy

The code for this article is available on GitHub

The code sample uses the numpy.isnat() method to check if a single value is NaT (not a time).

The method will return True if the value is NaT and False otherwise.

The same approach can be used to check if each value in an array is NaT.

main.py
import numpy as np arr = np.array(['NaT', 2, 4, 'NaT', 6], dtype='datetime64[D]') # ๐Ÿ‘‡๏ธ [ True False False True False] print(np.isnat(arr))

check array for nat in numpy

We directly passed the array to the numpy.nat() method.

The method returns True if the element is NaT and False otherwise.

Note that the numpy.isnat() method fails when checking for Pandas NaT.

main.py
import numpy as np import pandas as pd # โ›”๏ธ TypeError: ufunc 'isnat' is only defined for datetime and timedelta. print(np.isnat(pd.NaT))

If you need to be able to check for both pandas and NumPy NaT, use the pandas.isnull() method from the next subheading.

# Checking a Value or an Array for NaT with pandas.isnull()

You can also use the pandas.isnull method to check a value or an array for NaT.

Make sure you have the pandas module installed to be able to run the code sample.

shell
pip install numpy pandas # or with pip3 pip3 install numpy pandas

Now import the module and use the pandas.isnull() method.

main.py
import numpy as np import pandas as pd not_a_time = np.datetime64('nat') print(not_a_time) # ๐Ÿ‘‰๏ธ NaT print('-' * 50) if pd.isnull(not_a_time): # ๐Ÿ‘‡๏ธ this runs print('The value has a type of NaT') else: print('The value does NOT have a type of NaT')

checking value or array for nat using pandas isnull

The code for this article is available on GitHub

The pandas.isnull() method takes a scalar or an array-like object and indicates whether the values are missing:

  • NaN in numeric arrays.
  • None or NaN in object arrays.
  • NaT (not a time) in datetime objects.

You can also use the method to check an array for NaT values.

main.py
import numpy as np import pandas as pd arr = np.array(['NaT', 2, 4, 'NaT', 6], dtype='datetime64[D]') # ๐Ÿ‘‡๏ธ [ True False False True False] print(pd.isnull(arr))

check array for nat values using pandas isnull

A True value is returned for each array element that is NaT, otherwise, False is returned.

The pandas.isnull() method properly checks for both NumPy and Pandas NaT values, so it should be your preferred approach.

main.py
import numpy as np import pandas as pd print(pd.isnull(pd.NaT)) # ๐Ÿ‘‰๏ธ True print(pd.isnull(np.datetime64('nat'))) # ๐Ÿ‘‰๏ธ True

You might also see the pandas.isna() method being used.

main.py
import numpy as np import pandas as pd print(pd.isna(pd.NaT)) # ๐Ÿ‘‰๏ธ True print(pd.isna(np.datetime64('nat'))) # ๐Ÿ‘‰๏ธ True

The pandas.isna() method is just an alias for pandas.isnull().

# Checking a value for NaT by converting it to a string

You can also check a value for NaT by converting it to a string.

main.py
import numpy as np not_a_time = np.datetime64('nat') print(not_a_time) # ๐Ÿ‘‰๏ธ NaT print('-' * 50) if str(not_a_time) == 'NaT': # ๐Ÿ‘‡๏ธ this runs print('The value has a type of NaT') else: print('The value does NOT have a type of NaT')
The code for this article is available on GitHub

Converting a datetime object with a type of NaT to a string returns the string "NaT".

main.py
import numpy as np not_a_time = np.datetime64('nat') print(not_a_time) # ๐Ÿ‘‰๏ธ NaT print(repr(str(not_a_time))) # ๐Ÿ‘‰๏ธ 'NaT'

Therefore, we can compare the resulting string to "NaT".

main.py
import numpy as np not_a_time = np.datetime64('nat') print(not_a_time) # ๐Ÿ‘‰๏ธ NaT print(str(not_a_time) == 'NaT') # ๐Ÿ‘‰๏ธ True dt = np.datetime64('2024-09-24') print(str(dt) == 'NaT') # ๐Ÿ‘‰๏ธ False

The expression returns True if the value has a type of NaT and False otherwise.

However, note that this approach can only be used to check if a single value is NaT.

If you need to check if each value in an array is NaT, use the approach from the previous subheading.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev