转换为datetime会产生错误" ValueError:day超出了月份的范围"怎么找

时间:2016-11-29 02:46:59

标签: datetime pandas

我有一个巨大的数据框,看起来像这样:

                    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  

我试图通过此代码将索引dtype更改为日期时间dtype

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

但是它产生了错误:

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

有没有办法可以找到错误发生位置的索引? 我似乎可以将errors设置为coerce,这只会返回Nan作为日期,但我想避免这种情况。

谢谢!

1 个答案:

答案 0 :(得分:1)

你是对的,只需完成逻辑。设置为强制并过滤索引以对所有值isnull()以查找所有不正确的索引。