本文介绍了转换为日期时间时如何修复ValueError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时间数据与我的格式不匹配的错误。

An error in which timedata does not match my format.

这是数据示例:

import pandas as pd

data = pd.DataFrame({'TransactionTime': ['Sat Feb 02 12:50:00 IST 2019']})

这是我的代码:

data['TransactionTime'] = pd.to_datetime(data['TransactionTime'], format = '%a %b %d %H:%M:%S %Z %Y')



Traceback



Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
e:\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    431             try:
--> 432                 values, tz = conversion.datetime_to_datetime64(arg)
    433                 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-139-ed51a35d7ed3> in <module>
----> 1 data['TransactionTime'] = pd.to_datetime(data['TransactionTime'], format = '%a %b %d %H:%M:%S %Z %Y')

e:\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
    726             result = arg.map(cache_array)
    727         else:
--> 728             values = convert_listlike(arg._values, format)
    729             result = arg._constructor(values, index=arg.index, name=arg.name)
    730     elif isinstance(arg, (ABCDataFrame, abc.MutableMapping)):

e:\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    433                 return DatetimeIndex._simple_new(values, name=name, tz=tz)
    434             except (ValueError, TypeError):
--> 435                 raise e
    436 
    437     if result is None:

e:\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    398                 try:
    399                     result, timezones = array_strptime(
--> 400                         arg, format, exact=exact, errors=errors
    401                     )
    402                     if "%Z" in format or "%z" in format:

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

ValueError: time data 'Sat Feb 02 12:50:00 IST 2019' does not match format '%a %b %d %H:%M:%S %Z %Y' (match)


推荐答案

该错误很可能是由%Z 无法解析 IST 正确的时区。

most likely the error originates from the problem that %Z cannot parse IST to the correct timezone. There are multiple timezones that can be abbreviated as 'IST', so it is ambiguous anyway.

要解析例如: 'IST'到特定时区,您可以定义映射字典并将其提供给 dateutil 的parser.parse:

To parse e.g. 'IST' to a specific timezone, you can define a mapping dict and supply that to dateutil's parser.parse:

import pandas as pd
import dateutil

tzmap = {'IST': dateutil.tz.gettz('Asia/Kolkata')}

data = pd.DataFrame({'TransactionTime': ['Sat Feb 02 12:50:00 IST 2019']})

data['TransactionTime'] = data['TransactionTime'].apply(lambda t: dateutil.parser.parse(t, tzinfos=tzmap))

# data['TransactionTime']
# 0   2019-02-02 12:50:00+05:30
# Name: TransactionTime, dtype: datetime64[ns, tzfile('Asia/Calcutta')]

这篇关于转换为日期时间时如何修复ValueError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:46