TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced numpy numpy

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced


What's the dtype of temps. I can reproduce your warning and error with a string dtype:

In [26]: temps = np.array([1,2,'string',0])In [27]: tempsOut[27]: array(['1', '2', 'string', '0'], dtype='<U21')In [28]: temps==-99.9/usr/local/bin/ipython3:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison  #!/usr/bin/python3Out[28]: FalseIn [29]: np.isnan(temps)---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-29-2ff7754ed926> in <module>()----> 1 np.isnan(temps)TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

First, comparing strings with the number gives this future warning.

Second, testing for nan produces the error.

Note that given the dtype, the nan assignment assigns a string value, not a float (np.nan is a float).

In [30]: temps[-1] = np.nanIn [31]: tempsOut[31]: array(['1', '2', 'string', 'nan'], dtype='<U21')


isnan(ndarray) fails on ndarray dtype of "object"

isnan(ndarray.astype(np.float)), but strings cannot be coerced to float.


Note: This answer is somewhat related to the title of the question because this error prompts when working with Decimal types.

I got the same error when considering Decimal type values. For some reason, one column of the dataframe I'm considering comes as decimal. For example, when calling .unique() on this column I got

[Decimal('0'), Decimal('95'), Decimal('38'), Decimal('25'), Decimal('42'), Decimal('11'), Decimal('18'), Decimal('22'), .....Decimal('220'), Decimal('724')]

As the traceback of the error showed me that it failed when calling some numpy function. I manage to reproduce the error by considering the min and maxvalues of the above array

from decimal import Decimalxmin, xmax = Decimal('0'), Decimal('724')np.isnan([xmin, xmax])

it will prompt the error

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The solution in this case was to cast all these values to int.

df.astype({col:int for col in desired_columns_to_convert})