2

I'm having an issue converting some date fields when using pd.read_excel

cols_A8_J2007[i] = pd.read_excel(i, ('sheet'+str(j)), headers = 1, skiprows = 6, nrows=2000, usecols = 'A:J', converters = {'Expired': pd.to_datetime, 'Valid Until': pd.to_datetime})

I'd like to be able to pass errors='coerce' when the function is called, but I'm not sure how to do that as I only pass the function; so there must be some other argument to read_excel I have to pass in order to pass that when pd.to_datetime is called.

Pandas error trying to convert string into integer

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 4
    You can use a lambda: `'Valid Until': lambda x: pd.to_datetime(x, errors='coerce')` or [`functools.partial`](https://stackoverflow.com/questions/15331726/how-does-functools-partial-do-what-it-does/44814021) – pault Oct 15 '19 at 15:35
  • 1
    I would just do: `cols_A8_J2007[i]['Expired'] = pd.to_datetime(cols_A8..., errors='coerce')` after reading the dataframe with `read_excel`. – Quang Hoang Oct 15 '19 at 15:42

0 Answers0