'What to use for NaN Pandas when comparing with dates?

I have a pandas Series of NaN's and dates

eg.

# In the codebase this is being generated and used elsewhere, so changing the format would be a pain
x = pd.Series([
   np.nan, 
   np.nan, 
   pd.to_datetime('2020-01-01').date(),
   np.nan,
   pd.to_datetime('2020-02-01').date()
])

The questions is whether the most recent date (if there is one) is greater than '2021-06-01'

I can't do

x.max() > pd.to_datetime('2021-06-01')

because x.max() returns TypeError: '>=' not supported between instances of 'float' and 'datetime.date'

This is because pandas does not want to compare the float np.nan to a date.

However, if I do x.fillna(pd.NaT).max() it is still unhappy and throws warnings that np.NaT should not be compared to dates.

What is the appropriate way to do this in Pandas.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source