pandas parse_data出现异常时,自动跳过
生活随笔
收集整理的這篇文章主要介紹了
pandas parse_data出现异常时,自动跳过
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
當(dāng)處理原始數(shù)據(jù)時,出現(xiàn)下面的錯誤:
id,name,date 0,a,2020/01/01 0,b,2020/01/01 0,c,2020/01/01 0,d,2020/01/01 0,e,2020/01/01 0,f,9999/01/01用pandas進(jìn)行處理:
data = pandas.read_csv(file, sep=";", encoding="ISO-8859-1", parse_dates=["date"], date_parser=lambda x: pandas.to_datetime(x, format="%d.%m.%Y"))但是運(yùn)行時報錯,意思是out of bonds timestamp。
我們目前的做法是,需要跳過異常的行,
需要添加下面的行
date_parser=lambda x: pd.to_datetime(x, errors="coerce")errors參數(shù)共有三種賦值,默認(rèn)的值為‘raise’,出現(xiàn)不符合規(guī)范的解析時就會報錯。
- 可以將errors參數(shù)賦值為‘coerce’,在解析的過程中將出錯的時間格式設(shè)置為NaT。
- 如果不想處理錯誤的時間格式,可以將errors賦值為‘ignore’,這樣就還是原來的格式。
errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
- If ‘raise’, then invalid parsing will raise an exception.
- If ‘coerce’, then invalid parsing will be set as NaT.
- If ‘ignore’, then invalid parsing will return the input.
總結(jié)
以上是生活随笔為你收集整理的pandas parse_data出现异常时,自动跳过的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海马属于鱼类吗(海马是否属于鱼类)
- 下一篇: 广汽本田全新纯电车型将于广州车展亮相,预