«ValueError: непреобразованные данные остаются:» при преобразовании столбца pandas в следующий формат даты и времени '% Y% b% d% H:% M:% S'? - PullRequest
0 голосов
/ 09 мая 2020

Это мой код.

df_log['DateTime']= pd.to_datetime('2010 '+ df_log['DateTime'], format='%Y %b %d %H:%M:%S')

Вот как выглядит DataFrame:

DateTime    LogHost ApplicationProcess  LogMessage  AccessType
0   Jun 14 15:16:01 combo   sshd(pam_unix)[19939]   authentication failure; logname= uid=0 euid=0 ...   Unauthorized
1   Jun 14 15:16:02 combo   sshd(pam_unix)[19937]   check pass; user unknown    NaN
2   Jun 14 15:16:02 combo   sshd(pam_unix)[19937]   authentication failure; logname= uid=0 euid=0 ...   Unauthorized
3   Jun 15 02:04:59 combo   sshd(pam_unix)[20882]   authentication failure; logname= uid=0 euid=0 ...   Unauthorized
4   Jun 15 02:04:59 combo   sshd(pam_unix)[20884]   authentication failure; logname= uid=0 euid=0 ...   Unauthorized

Мой идеальный выход - преобразовать столбец DateTime из объекта в datetime64 [ns]

Однако я продолжаю получать сообщение об ошибке, как показано

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, box, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    447             try:
--> 448                 values, tz = conversion.datetime_to_datetime64(arg)
    449                 return DatetimeIndex._simple_new(values, name=name, tz=tz)

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()

TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-277-9b47aa7f900a> in <module>
      7 #pd.Timestamp.min
      8 #pd.Timestamp.max
----> 9 df_log['DateTime']= pd.to_datetime('2010 '+ df_log['DateTime'], format='%Y %b %d %H:%M:%S')
     10 
     11 df_log.info()

~/opt/anaconda3/lib/python3.7/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)
    772                 result = result.tz_localize(tz)
    773     elif isinstance(arg, ABCSeries):
--> 774         cache_array = _maybe_cache(arg, format, cache, convert_listlike)
    775         if not cache_array.empty:
    776             result = arg.map(cache_array)

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
    154         unique_dates = unique(arg)
    155         if len(unique_dates) < len(arg):
--> 156             cache_dates = convert_listlike(unique_dates, True, format)
    157             cache_array = Series(cache_dates, index=unique_dates)
    158     return cache_array

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, box, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    449                 return DatetimeIndex._simple_new(values, name=name, tz=tz)
    450             except (ValueError, TypeError):
--> 451                 raise e
    452 
    453     if result is None:

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, box, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    414                 try:
    415                     result, timezones = array_strptime(
--> 416                         arg, format, exact=exact, errors=errors
    417                     )
    418                     if "%Z" in format or "%z" in format:

pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()

ValueError: unconverted data remains:  

Есть какие-нибудь советы, как решить эту проблему? Любая помощь будет оценена по достоинству.

1 Ответ

0 голосов
/ 09 мая 2020
  • Если я добавлю 'Jun 15 02:04:59 ' в столбец Datetime, я получу ту же ошибку. Итак, я предполагаю, что в вашем столбце DateTime есть пробелы.
  • Чтобы решить проблему:
    • Используйте .str.strip(), чтобы удалить пробелы
    • Удалите параметр format
import pandas as pd

# data
data = {'DateTime': ['Jun 14 15:16:01', 'Jun 14 15:16:02', 'Jun 14 15:16:02', 'Jun 15 02:04:59', 'Jun 15 02:04:59 ']}

# dataframe
df = pd.DataFrame(data)

         DateTime
  Jun 14 15:16:01
  Jun 14 15:16:02
  Jun 14 15:16:02
  Jun 15 02:04:59
 Jun 15 02:04:59 

print(type(df.DateTime[0]))

>>> <class 'str'>

# original method
df['original method'] = pd.to_datetime('2010 '+ df['DateTime'].str.strip(), format='%Y %b %d %H:%M:%S')

# alternate method
df['alternate method'] = pd.to_datetime('2010 '+ df['DateTime'])

print(df)

           DateTime     original method    alternate method
    Jun 14 15:16:01 2010-06-14 15:16:01 2010-06-14 15:16:01
    Jun 14 15:16:02 2010-06-14 15:16:02 2010-06-14 15:16:02
    Jun 14 15:16:02 2010-06-14 15:16:02 2010-06-14 15:16:02
    Jun 15 02:04:59 2010-06-15 02:04:59 2010-06-15 02:04:59
 Jun  15  02:04:59  2010-06-15 02:04:59 2010-06-15 02:04:59
...