Replace str values in series into np.nan

I have the following series

s = pd.Series({'A':['hey','hey',2,2.14},index=1,2,3,4)

I basically want to mask, the series and check if the values are a str if so i want to replace then with np.nan, how could i achieve that?

Wanted result

s = pd.Series({'A':[np.nan,np.nan,2,2.14},index=1,2,3,4)

I tried this

s.mask(isinstance(s,str))

But i got the following ValueError: Array conditional must be same shape as self, i am kinda a newb when it comes to these methods would appreciate a explanation on the why

>Solution :

You can use

out = s.mask(s.apply(type).eq(str))
print(out)

1     NaN
2     NaN
3       2
4    2.14
dtype: object

Leave a Reply