Efficiently checking if arbitrary object is NaN in Python / numpy / pandas?

Efficiently checking if arbitrary object is NaN in Python / numpy / pandas?

Asked on December 21, 2018 in Pandas.
Add Comment


  • 1 Answer(s)

    For checking the missing values in numeric as well as object/string arrays use the function pandas.isnull(). The output will be:

    NaN in numeric arrays, None/NaN in object arrays

    import pandas as pd
    import numpy as np
    s = pd.Series(['apple', np.nan, 'banana'])
    pd.isnull(s)
    Out[9]:
    0 False
    1  True
    2 False
    dtype: bool
    

    The above code is an example.

    To represent the missing values in pandas, use the function numpy.nan and pandas has the tools to work with.

    If the function pd.NaT is used the dtype is need not to be mentioned.

    In [24]: s = Series([Timestamp('20130101'),np.nan,Timestamp('20130102 9:30')],dtype='M8[ns]')
     
    In [25]: s
    Out[25]:
    0 2013-01-01 00:00:00
    1                 NaT
    2 2013-01-02 09:30:00
    dtype: datetime64[ns]``
     
    In [26]: pd.isnull(s)
    Out[26]:
    0 False
    1  True
    2 False
    dtype: bool
    
    Answered on December 21, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.