What does the isnull() function work on?

The project question asked me to determine the number of NaT values in a specific column of a dataframe. My initial idea was to create a counter that would iterate through the column and +=1 wherever a NaT value existed. The pandas documentation mentions that the .isnull() function can take in a scalar or array and return a boolean value or array of boolean values.

I was hoping to use the boolean value to fulfil my conditional statement but it gives me the error 'NaTType object has no attribute .isnull()?

I understand that the other solution is quicker but why doesn’t this work? Thanks in advance :slight_smile:

# this is the solution given in the project solutions
print(len(visits_carts[visits_carts.cart_time.isnull()]))

#before I went to the solutions I tried this 

count = 0
for i in range(len(visits_carts)):
    if visits_carts.loc[i, "cart_time"].isnull():
        count+=1

print(count)

With Pandas, there’s a few ways you could determine if there are any nulls in the dataframe and by column:

#the entire df:

df.isnull().sum()
>>age         0
sex         0
bmi         0
children    0
smoker      0
region      0
charges     0
dtype: int64

#the total sum by df:
df.isnull().sum().sum()

>>0

#the sum of nulls by column:
df.charges.isnull().sum()

>>0

#the boolean way:
df.isnull().values.any()

>>False

#by column:
df.column_name.isnull().sum()

>>0

You can always check the docs:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isnull.html