pythonpandasdataframedatetime64

How to handle missing value datetime64[ns] dtype column for Python Pandas DataFrame?


If I have data something like this, a missing value in 'Date4' column, its a datetime64[ns] dtype.

I have searched for the solution on several websites but couldn't get proper answer yet.

   No  Name      Date1      Date2      Date3      Date4
0   1  Per1 2015-05-25 2016-03-20 2016-03-22 2017-01-01
1   2  Per2 2015-06-26 2016-05-22 2016-06-22 2017-02-02
2   3  Per3 2015-09-28 2016-07-24 2016-07-26 2017-05-22
3   4  Per4 2015-11-21 2016-09-02 2016-05-09 2017-05-22
4   5  Per5 2015-12-25 2016-11-11 2016-11-14        NaT

Solution

  • In [135]: df
    Out[135]:
           Date4
    0 2017-01-01
    1 2017-02-02
    2 2017-05-22
    3 2017-05-22
    4        NaT
    
    In [136]: df["Date4"].replace(np.nan, df["Date4"].mode().iloc[0])
    Out[136]:
    0   2017-01-01
    1   2017-02-02
    2   2017-05-22
    3   2017-05-22
    4   2017-05-22
    Name: Date4, dtype: datetime64[ns]