0

Given a numpy array, I want to identify which rows contain NaN values and objects. For example, a row would contain both a float value and a list.

For the input array arr, I tried doing arr[~np.isnan(arr).any(axis=1)] but then I get 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''
5
  • 1
    You can try doing arr[~np.isnan(arr, casting = 'no').any(axis=1)] or arr[~np.isnan(arr, casting = 'unsafe').any(axis=1)] Commented Jul 24, 2019 at 15:32
  • seems like you have different dtypes in your array. np.isnan is intended for nummeric, floating point type arrays - so maybe you can pre-select that part of your array which can be converted to float bevor applying np.isnan? Commented Jul 24, 2019 at 15:38
  • @SayandipDutta, I tried your suggestions on np.array([5., np.nan, 'asdf']) - gives me a similar / the same TypeError as @Anonymous experiences. Commented Jul 24, 2019 at 15:41
  • Given an example array. I assume the dtype is object. Commented Jul 24, 2019 at 16:03
  • For example, if the array is np.array([[1, [2,3], np.nan], [3, [5,6,7], 8]]), I would want to get rid of the first row. Commented Jul 24, 2019 at 17:02

1 Answer 1

1
In [314]: x = np.array([[1, [2,3], np.nan], [3, [5,6,7], 8]])                                                
In [315]: x                                                                                                  
Out[315]: 
array([[1, list([2, 3]), nan],
       [3, list([5, 6, 7]), 8]], dtype=object)
In [316]: x.shape                                                                                            
Out[316]: (2, 3)
In [317]: x[0]                                                                                               
Out[317]: array([1, list([2, 3]), nan], dtype=object)
In [318]: x[1]                                                                                               
Out[318]: array([3, list([5, 6, 7]), 8], dtype=object)

isnan works on a float dtype array; object dtype can't be converted to that:

In [320]: np.isnan(x)                                                                                        
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-320-3b2be83a8ed7> in <module>
----> 1 np.isnan(x)

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

We can, though, test the elements one by one with a is np.nan test:

In [325]: np.frompyfunc(lambda i: i is np.nan,1,1)(x)                                                        
Out[325]: 
array([[False, False, True],
       [False, False, False]], dtype=object)

frompyfunc returns an object dtype; let's convert that to bool:

In [328]: np.frompyfunc(lambda i: i is np.nan,1,1)(x).astype(bool)                                           
Out[328]: 
array([[False, False,  True],
       [False, False, False]])
In [329]: np.any(_, axis=1)           # test whole rows                                                                       
Out[329]: array([ True, False])
In [330]: x[~_, :]                    # use that as mask to keep other rows                                                      
Out[330]: array([[3, list([5, 6, 7]), 8]], dtype=object)

The pandas isnull suggested in the other answer, can do a similar element by element test:

In [335]: pd.isnull(x)                                                                                       
Out[335]: 
array([[False, False,  True],
       [False, False, False]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.