Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Working example
df_working = pd.DataFrame({'A': ['test', np.nan, np.nan], "B": [1, 2, 3]})
df_working['A'] = df_working['A'].str.contains('t', na=False)

# Not working example
df_not_working = pd.DataFrame({'A': [np.nan, np.nan], "B": [1, 2]})
df_not_working['A'] = df_not_working['A'].str.contains('t', na=False)
#AttributeError: Can only use .str accessor with string values!

Issue Description

In 'not working example', column A is build by nans and str.contains(na=False) is producing AttributeError. When this column contains at least one string like in 'working example' this function works good.

So in general str.contains(na=False) or na=True is not working on nans only.

Expected Behavior

df = pd.DataFrame({'A': [np.nan, np.nan], "B": [1, 2]})
df['A'] = df['A'].str.contains('t', na=False)

expected output:

df :

       A  B
0  False  1
1  False  2

Installed Versions

pandas : 1.5.2

Comment From: samukweku

What is the dtype of the column that contains all Nan's? Float64? String dtype? String functions work only on string dtype columns

Comment From: phofl

Yes, the column has float64 dtype, this is not expected to work. You have to cast to object or string if you want to use str accessors