代码之家  ›  专栏  ›  技术社区  ›  kms

将字符串转换为dataframe中的日期并处理错误值

  •  0
  • kms  · 技术社区  · 3 年前

    我有一个熊猫数据框,日期存储为字符串。有一些坏数据在那里,这是抛出一个错误,当我试图将它们转换为日期。

    以下是数据:

    df['strDates']
    
    0       19990510
    1       20150303
    2       20090908
    3       19920200  <-- Bad Date
    4       20030530
    

    当我跑的时候,

    df['lastSaleDate'] = pd.to_datetime(df['lastSaleDate'], format='%Y%m%d')
    

    我得到一个错误:

        /Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py in _array_strptime_with_fallback(arg, name, tz, fmt, exact, errors, infer_datetime_format)
        434 
        435     try:
    --> 436         result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
        437         if "%Z" in fmt or "%z" in fmt:
        438             return _return_parsed_timezone_results(result, timezones, tz, name)
    
    /Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
    
    ValueError: time data '19920200' does not match format '%Y%m%d' (match)
    
    2 回复  |  直到 3 年前
        1
  •  2
  •   wwnde    3 年前

    只是强迫dropna把它处理掉。 pd.to_datetime(df['lastSaleDate'],format='%Y%m%d',errors='coerce')