The Great
The Great

Reputation: 7733

How useful it is to convert nan to NaN?

I am trying to run a code which is already written by someone else. However, I am not sure what's the need to convert nan to NaN

A piece of code line regarding the issue

obs['valuestring'].astype(str).str.strip().mask(obs['valuestring'].isna())

The above line code gives an output like this

enter image description here

But what I feel is the piece of code below should be fine. Isn't it?

obs['valuestring'].astype(str).str.strip()

and it produces the same output as above but nan instead of NaN

I understand that np.nan is np.NaN, so trying to learn is there anything useful with this and am I missing it?

Can you help me understand the need to convert nan to NaN and when should I do these kind of conversions?

enter image description here

Upvotes: 2

Views: 102

Answers (1)

jezrael
jezrael

Reputation: 863186

It is difference, because in second have no missing values, but string reperesentation of NaN - string nan:

obs = pd.DataFrame({'valuestring':['a ', np.nan, np.nan]})
print (obs)
  valuestring
0          a 
1         NaN
2         NaN

s = obs['valuestring'].astype(str).str.strip().mask(obs['valuestring'].isna())
print (s)
0      a
1    NaN
2    NaN
Name: valuestring, dtype: object

print (s.isna())
0    False
1     True
2     True
Name: valuestring, dtype: bool

s = obs['valuestring'].astype(str).str.strip()
print (s)
0      a
1    nan
2    nan
Name: valuestring, dtype: object

print (s.isna())
0    False
1    False
2    False
Name: valuestring, dtype: bool

print (s.eq('nan'))
0    False
1     True
2     True
Name: valuestring, dtype: bool

Upvotes: 3

Related Questions