I am trying to take in turn the maximum (and min) of two columns containing dates
from datetime import date
import pandas as pd
import numpy as np
df = pd.DataFrame({'date_a' : [date(2015, 1, 1), date(2012, 6, 1),
date(2013, 1, 1), date(2016, 6, 1)],
'date_b' : [date(2012, 7, 1), date(2013, 1, 1),
date(2014, 3, 1), date(2013, 4, 1)]})
df[['date_a', 'date_b']].max(axis=1)
Out[46]:
0 2015-01-01
1 2013-01-01
2 2014-03-01
3 2016-06-01
as expected. However, if the data frame contains one NaN value, the whole operation fails.
df_nan = pd.DataFrame({'date_a' : [date(2015, 1, 1), date(2012, 6, 1),
np.NaN, date(2016, 6, 1)],
'date_b' : [date(2012, 7, 1), date(2013, 1, 1),
date(2014, 3, 1), date(2013, 4, 1)]})
df_nan[['date_a', 'date_b']].max(axis=1)
Out[49]:
0 NaN
1 NaN
2 NaN
3 NaN
dtype: float64
What's going on here? I expected this result
0 2015-01-01
1 2013-01-01
2 NaN
3 2016-06-01
How to achieve this?