How to ignore pd.NaT in max?

Viewed 1144

Short: how to ignore pd.NaT in a list put into max?

import datetime
max([pd.NaT, datetime.datetime(1900, 1, 1), datetime.datetime(2000, 1, 1)])

The expected output is Timestamp('2000-01-01 00:00:00'). Even if this question seems to be a standard problem, I couldn't find a solution beside this unpythonic one:

max(pd.DataFrame([pd.NaT, datetime.datetime(1900, 1, 1), datetime.datetime(2000, 1, 1)]).dropna()[0])
1 Answers

Right now you are using pyhton's max function. Instead you can use pandas series max which automatically handles NaT:

pd.Series([pd.NaT, datetime.datetime(1900, 1, 1), datetime.datetime(2000, 1, 1)]).max()
Related