Reputation: 21961
How can I validate for NaT in python while at the same time working for timestamps. E.g. the variable _date
can be either NaT
or Timestamp('2017-12-02 00:00:00')
If I use this: np.isnat(np.datetime64(_date))
, it works for Timestamp('2017-12-02 00:00:00')
but not NaT
Upvotes: 2
Views: 8077
Reputation: 323226
By using to_datetime
time=pd.to_datetime(time)
time
Out[1131]:
0 2017-12-02 20:40:30
1 2017-12-02 00:00:00
2 NaT
dtype: datetime64[ns]
Upvotes: 0
Reputation: 721
You can use isna
or fillna
method on it
import pandas as pd
import numpy as np
time = pd.Series(['2017-12-02 20:40:30','2017-12-02 00:00:00',np.nan])
time = time.apply(lambda x: pd.Timestamp(x))
print(time)
0 2017-12-02 20:40:30
1 2017-12-02 00:00:00
2 NaT
time.isna()
0 False
1 False
2 True
time.fillna("missing")
0 2017-12-02 20:40:30
1 2017-12-02 00:00:00
2 missing
Upvotes: 3