user308827
user308827

Reputation: 21961

Checking for both NaT or pandas timestamp

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

Answers (2)

BENY
BENY

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

Sahil Dahiya
Sahil Dahiya

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

Related Questions