Moondra
Moondra

Reputation: 4511

Conversion to datetime is yielding error "ValueError: day is out of range for month" How to find

I have a huge dataframe that looks something like this:

                    Insider Trading                   Relationship    Date  \
SEC Form 4                                                                   
Nov 16 04:06 PM      Silverman Gene                       Director  Nov 14   
Oct 27 07:00 AM     RAKOLTA JOHN JR                       Director  Oct 26   
Nov 16 04:09 PM  LEIGHTON F THOMSON        Chief Executive Officer  Nov 15   
Nov 02 04:20 PM      Blumofe Robert                   EVP Platform  Nov 01   
Oct 28 04:03 PM    MCCONNELL RICK M  President Prods & Development  Oct 28  

I'm trying to change the index dtype into a datetime dtype via this code

pd.to_datetime(df2.index, format = '%b %d %I:%M %p')

but it's yielding the error:

Traceback (most recent call last):
  File "<pyshell#126>", line 1, in <module>
    pd.to_datetime(df2.index, format = '%b %d %I:%M %p')
  File "C:\Python27\lib\site-packages\pandas\util\decorators.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pandas\tseries\tools.py", line 420, in to_datetime
    return _convert_listlike(arg, box, format, name=arg.name)
  File "C:\Python27\lib\site-packages\pandas\tseries\tools.py", line 407, in _convert_listlike
raise e

Is there a way I can find the index of where the error is occurring? It seems I can set errors to coerce which would just return a Nan as the date, but I would like to avoid that.

Thanks!

Upvotes: 0

Views: 343

Answers (1)

Zeugma
Zeugma

Reputation: 32095

You are right, just finish the logic. Set to coerce and filter the index against all values being isnull() to find all the incorrect indices.

Upvotes: 1

Related Questions