Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing nan with 0 error:nan not supported for input type

Tags:

python

numpy

I have a numpy array which contains date and time as follows

a[0]=2014-06-01 03:14:34

I checked type of a[0]. It is given to be str.Some values in numpy array are nan. I have to replace nan with 0 and rest with 1. I tried isnan(a) but it gave me 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'' What is wrong and how can I replace nan with 0?

like image 561
Aashish sharma Sharma Avatar asked Dec 09 '25 18:12

Aashish sharma Sharma


2 Answers

In order for your array to contain both strings and NaNs, it must be of object dtype. NumPy arrays of object dtype are not particularly efficient in terms of memory or performance. There are no vectorized operations for object arrays. The error message

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''

is saying that np.isnan can not be applied to object arrays, and this object array could not be coerced to a supported data type such as np.float.

Ideally, avoid building object arrays in the first place. But given that you have this object array, to build the new array, you might as well just loop over the items in a in a list comprehension:

import numpy as np
a = np.array(['2014-06-01 03:14:34', np.nan], dtype='O')
b = np.array([item == item for item in a]).astype(int)
print(b)

yields

[1 0]

How item == item tests for NaN:

NaN has the property that NaN != NaN. NaN is the only object in the pantheon of standard Python objects which has this property. Custom classes can be defined whose instances can also have this property. As long as a does not contains instances of such a custom class, you could use the item != item property to test for NaN:

In [43]: float('nan') != float('nan')
Out[43]: True

In [44]: np.nan != np.nan
Out[44]: True
like image 184
unutbu Avatar answered Dec 11 '25 07:12

unutbu


In my case, I had to create array with preset size with np.zeros then fill the cells manually to create vectorized numpy array that I can use with isnan.

In the code below, fields contains list of field names used in object array.

v_data = np.zeros((o_data.shape[0], len(fields)))
for rcounter, row in enumerate(o_data):
  for fcounter, field in enumerate(fields):
    v_data[rcounter, fcounter] = row[field]
like image 25
jaycode Avatar answered Dec 11 '25 07:12

jaycode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!