Aashish sharma Sharma
Aashish sharma Sharma

Reputation: 307

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

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?

Upvotes: 3

Views: 9159

Answers (3)

jaycode
jaycode

Reputation: 2958

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]

Upvotes: 1

unutbu
unutbu

Reputation: 880927

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

Upvotes: 9

mmachine
mmachine

Reputation: 926

You may use function:

def replace_nan(num):
    if not numpy.nan_to_num(num):
        return 1

Upvotes: 0

Related Questions