[英]to_datetime Unrecognized value type: <class 'str'>
I am trying to convert the 'date' column from string to dateime data type我正在尝试将“日期”列从字符串转换为日期时间数据类型
example of the 'date' column: “日期”列的示例:
array(['2015-08-26 10:24:48.127', '2015-08-26 10:26:41.000',
'2015-08-26 10:27:52.000', ..., '2015-08-26 10:18:11.000',
'2015-08-26 10:21:39.000', '2015-08-26 10:23:05.000'], dtype=object)
Attempt at converting to datetime data type: To get the date component from the date column The date column in YYYY-MM-DD format.尝试转换为日期时间数据类型:从日期列中获取日期组件 YYYY-MM-DD 格式的日期列。
order_items['date']= pd.to_datetime('date', format="%Y-%m-%d")```
However i got the following error:
```TypeError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/arrays/datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
1857 try:
-> 1858 values, tz_parsed = conversion.datetime_to_datetime64(data)
1859 # If tzaware, these values represent unix timestamps, so we
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>```
you are passing date
literal to the pd.to_datetime
function, not the actual date.您将date
文字传递给pd.to_datetime
function,而不是实际日期。
if you do something like -如果你做类似的事情 -
pd.to_datetime('2015-08-26 10:24:48.127', format="%Y-%m-%d")
It should work.它应该工作。
Or if it is a column in dataframe(assuming named df)或者如果它是数据框中的一列(假设名为 df)
pd.to_datetime(df['date'], format="%Y-%m-%d")
You actually need to pass the correct format , the format you have passed to the datetime
function doesn't match the records that your array has, and and the correct format for the data you have is %Y-%m-%d %H:%M:%S.%f
:您实际上需要传递正确的格式,您传递给datetime
时间 function 的格式与您的数组具有的记录不匹配,并且您拥有的数据的正确格式是%Y-%m-%d %H:%M:%S.%f
:
pd.to_datetime(data, format='%Y-%m-%d %H:%M:%S.%f')
#Output
DatetimeIndex(['2015-08-26 10:24:48.127000', '2015-08-26 10:26:41',
'2015-08-26 10:27:52', '2015-08-26 10:18:11',
'2015-08-26 10:21:39', '2015-08-26 10:23:05'],
dtype='datetime64[ns]', freq=None)
When passing the date
to pd.to_dateframe
you did not select the column.将date
传递给pd.to_dateframe
时,您没有 select 列。
The below code gives a df['date']
column which consists of datetime objects.下面的代码给出了一个由 datetime 对象组成的df['date']
列。
import pandas as pd
date_dict= {'date': ['2015-08-26 10:24:48.127', '2015-08-26 10:26:41.000',
'2015-08-26 10:27:52.000', '2015-08-26 10:18:11.000',
'2015-08-26 10:21:39.000', '2015-08-26 10:23:05.000']}
df = pd.DataFrame(data=date_dict)
df['date']= pd.to_datetime(df['date'], format="%Y-%m-%d %H:%M:%S.%f")
print(df['date'])
prints印刷
0 2015-08-26 10:24:48.127
1 2015-08-26 10:26:41.000
2 2015-08-26 10:27:52.000
3 2015-08-26 10:18:11.000
4 2015-08-26 10:21:39.000
5 2015-08-26 10:23:05.000
Name: date, dtype: datetime64[ns]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.