【问题标题】:Remove NaN rows from numpy array that also contain objects从也包含对象的 numpy 数组中删除 NaN 行
【发布时间】:2019-07-25 02:17:50
【问题描述】:

给定一个 numpy 数组,我想确定哪些行包含 NaN 值和对象。 例如,一行将包含一个浮点值和一个列表。

对于输入数组arr,我尝试执行arr[~np.isnan(arr).any(axis=1)],但随后收到错误消息

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

【问题讨论】:

  • 你可以试试arr[~np.isnan(arr, casting = 'no').any(axis=1)]arr[~np.isnan(arr, casting = 'unsafe').any(axis=1)]
  • 似乎您的数组中有不同的数据类型。 np.isnan 适用于数字、浮点类型数组 - 所以也许您可以预先选择可以转换为浮点的数组部分,然后应用 np.isnan
  • @SayanipDutta,我在np.array([5., np.nan, 'asdf']) 上尝试了你的建议 - 给了我与@Anonymous 经验相似/相同的TypeError
  • 给定一个示例数组。我假设 dtype 是object
  • 比如数组是np.array([[1, [2,3], np.nan], [3, [5,6,7], 8]]),我想去掉第一行。

标签: python numpy


【解决方案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 适用于 float dtype 数组; object dtype 无法转换为:

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

不过,我们可以使用 is np.nan 测试一个一个地测试元素:

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 返回一个对象 dtype;让我们将其转换为 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)

另一个答案中建议的熊猫isnull,可以逐个元素地进行类似的测试:

In [335]: pd.isnull(x)                                                                                       
Out[335]: 
array([[False, False,  True],
       [False, False, False]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2015-05-21
    • 2019-12-22
    • 2015-06-08
    • 2020-06-19
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多