Apply sign function to a column that contains NaN values

Viewed 23

I have a column that contains both valid numbers as well as NaN. I want to apply sign function on its values and keep the result in a new column.

When I use np.sign as follows, it fails saying: {TypeError} unorderable types for comparison.

df['new_column'] = np.sign(df['column'])

Note that I cannot replace NaN with 0 or something beforehand. In my case if it is a NaN, its sign should be NaN as well.

1 Answers

Could you post some data to reproduce the problem? For example: dtype, values, and what kind of "NaN" you are using.

The problem can be anywhere, have a look here:

As you can see, no problem with np.nan:

np.sign([53,54,512,3,np.nan])
Out[16]: array([ 1.,  1.,  1.,  1., nan])

But pd.NA does not work (another exception than in your case):

np.sign([53,54,512,3,pd.NA])
Traceback (most recent call last):
File "C:\Users\Gamer\anaconda3\envs\nlp\lib\site- 
packages\IPython\core\interactiveshell.py", line 3251, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-22d6e4b28023>", line 1, in <module>
np.sign([53,54,512,3,pd.NA])
File "pandas\_libs\missing.pyx", line 382, in 
pandas._libs.missing.NAType.__bool__
TypeError: boolean value of NA is ambiguous

Some data would be useful.

Related