How to Fix AttributeError: ‘NaTType’ object has no attribute ‘isna’

Diagram of How to Fix AttributeError: 'NaTType' object has no attribute 'isna'

Diagram

AttributeError: ‘NaTType’ object has no attribute ‘isna’ error occurs when you are trying to “call the isna() method on an object of type NaTType, which is the data type for pandas Not a Time value (NaT).”

“NaT” (for date/time types) and “NaN” are not the same.

How to fix it?

To fix the AttributeError: ‘NaTType’ object has no attribute ‘isna’ error, you can use the “pd.isnull()” method.

import pandas as pd

value = pd.NaT
is_nat = pd.isnull(value)

print(is_nat)

Output

True

The pd.isnull() method accepts any pandas object as input and returns True if the object is missing and False otherwise.

Another correct way to check if an object is NaT in pandas is to use the “pd.isna()” method.

import pandas as pd

value = pd.NaT
is_nat = pd.isna(value)

print(is_nat)

Output

True

The mistake was trying to call isna() directly on the NaT object. Instead, use the “pd.isna()” function as demonstrated.

That’s it!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.