gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:24 26 4
gpt4 key购买 nike

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

 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

我想制作像7/1/2018这样的格式,所以我用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)

我该如何处理这个错误?

最佳答案

format pd.to_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

关于python - 将数据集中的列类型转换为python中特定格式的日期时间类型时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54315212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com