将数据集中的列类型转换为python中具有特定格式的日期时间类型时出错

时间:2019-01-22 19:31:49

标签: python pandas datetime format

我有一个数据集,我想更改名为“上次更新”的列的格式。

 DB['Last Updated'].head()

 0     January 7, 2018
 1    January 15, 2018
 2      August 1, 2018
 3        June 8, 2018
 4       June 20, 2018
Name: Last Updated, dtype: object

我想将格式设置为2018年7月1日,因此我在python中编写了以下内容。

 DB['Last Updated'] = pd.to_datetime(DB['Last Updated'],format= '%d/%m/%Y')

但是出现此错误:

 TypeError                                 Traceback (most recent call last) ~/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
 302             try:
 --> 303                 values, tz = tslib.datetime_to_datetime64(arg)
304                 return DatetimeIndex._simple_new(values, name=name, tz=tz)

pandas/_libs/tslib.pyx in pandas._libs.tslib.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-62-1dd2ca5f727a> in <module>()
 ----> 1 DB['Last Updated'] = pd.to_datetime(DB['Last Updated'],format= '%d/%m/%Y')

~/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin)
371     elif isinstance(arg, ABCSeries):
372         from pandas import Series
--> 373         values = _convert_listlike(arg._values, True, format)
374         result = Series(values, index=arg.index, name=arg.name)
375     elif isinstance(arg, (ABCDataFrame, MutableMapping)):

~/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
304                 return DatetimeIndex._simple_new(values, name=name, tz=tz)
305             except (ValueError, TypeError):
--> 306                 raise e
307 
308     if arg is None:

~/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
271                     try:
272                         result = array_strptime(arg, format, exact=exact,
--> 273                                                 errors=errors)
274                     except tslib.OutOfBoundsDatetime:
275                         if errors == 'raise':

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

 ValueError: time data 'January 7, 2018' does not match format '%d/%m/%Y' (match)

如何处理此错误?

1 个答案:

答案 0 :(得分:0)

format中的pd.to_datetime(...)参数用于指定要从其转换的字符串的格式(而不是指定输出格式)。为了将日期字符串转换为datetime对象,然后转换为特定的输出格式,您可以执行以下操作:

import pandas as pd

data = [{'Last Updated': 'January 7, 2018'}, {'Last Updated': 'January 15, 2018'}]
df = pd.DataFrame(data)

df['Last Updated'] = pd.to_datetime(df['Last Updated'])
df['Last Updated'] = df['Last Updated'].dt.strftime('%d/%m/%Y')

print(df)
# OUTPUT
#   Last Updated
# 0   07/01/2018
# 1   15/01/2018