Skip to content Skip to sidebar Skip to footer

Typeerror: Bad Operand Type For Unary ~: 'float'

I have a weird looking dataframe that I need to wrangle. It looks something like this: Unnamed: 0 REFERENCE_CODE ... Unnamed: 12 Unnamed: 13 0 Q2 countr

Solution 1:

You can fillna first:

~df.REFERENCE_CODE.fillna('').str.isnumeric()

Example:

In [11]: s = pd.Series(['1', np.nan, 'c'])

In [12]: s
Out[12]:
011    NaN
2      c
dtype: object

In [13]: s.str.isnumeric()
Out[13]:
0True1      NaN
2False
dtype: object

In [14]: ~s.str.isnumeric()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-2e51f8bd1622> in <module>()
----> 1 ~s.str.isnumeric()

~/.miniconda3/lib/python3.7/site-packages/pandas/core/generic.py in __invert__(self)
   1141     def __invert__(self):
   1142try:
-> 1143             arr = operator.inv(com._values_from_object(self))
   1144returnself.__array_wrap__(arr)
   1145         except Exception:

TypeError: bad operand type for unary ~: 'float'

In [15]: ~s.fillna('').str.isnumeric()
Out[15]:
0False1True2True
dtype: bool

Post a Comment for "Typeerror: Bad Operand Type For Unary ~: 'float'"