0

I want to compare dates with NaNs included.

I used np.nanmin because this function excludes NaNs. However, this does not seem to work with dates.

I could write an apply() function, but because of the size of the dataset I would rather like to use a numpy-function. Any suggestions?

df = pd.DataFrame(np.array([[dt.datetime(2017, 1, 1, 0, 0),
dt.datetime(2017, 2, 1, 0, 0)],dt.datetime(2017, 3, 1, 0, 0), np.nan]]),
                   columns=['date1', 'date2'])

np.nanmin([df['date1'], df['date2']], axis=0)

I expect:

['2017-01-01', '2017-02-01']

I got:

ValueError: cannot convert float NaN to integer
Carsten
  • 2,765
  • 1
  • 13
  • 28
  • Use `df[['date1','date2']].min(axis=1)` – jezrael May 07 '19 at 13:29
  • Thank you, this works! Still, I don't understand why np.nanmin() creates this error. If I do the same procedure with numbers (and NaNs) or dates (without NaNs), np.nanmin() works fine. – Carsten May 07 '19 at 14:42

0 Answers0