Numpy isnan() fails on an array of floats (from pandas dataframe apply)

后端 未结 4 1031
清酒与你
清酒与你 2020-11-30 21:04

I have an array of floats (some normal numbers, some nans) that is coming out of an apply on a pandas dataframe.

For some reason, numpy.isnan is failing on this arra

4条回答
  •  一向
    一向 (楼主)
    2020-11-30 21:27

    np.isnan can be applied to NumPy arrays of native dtype (such as np.float64):

    In [99]: np.isnan(np.array([np.nan, 0], dtype=np.float64))
    Out[99]: array([ True, False], dtype=bool)
    

    but raises TypeError when applied to object arrays:

    In [96]: np.isnan(np.array([np.nan, 0], dtype=object))
    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''
    

    Since you have Pandas, you could use pd.isnull instead -- it can accept NumPy arrays of object or native dtypes:

    In [97]: pd.isnull(np.array([np.nan, 0], dtype=float))
    Out[97]: array([ True, False], dtype=bool)
    
    In [98]: pd.isnull(np.array([np.nan, 0], dtype=object))
    Out[98]: array([ True, False], dtype=bool)
    

    Note that None is also considered a null value in object arrays.

提交回复
热议问题