我目前正在尝试将Pandas列转换为日期时间列,以便计算出三组日期列之间的差异(1.酒店搜索日期,2。入住日期,3。出发日期)
以下是它的外观示例:
>>> print(df2)
date Arrive Depart
20180516
20180516
20180518 6172018 6242018
20180515
20180519
20180517
20180515 6052018 6062018
20180517 8132018 8162018
20180515 7112018 7152018
20180517 7272018 8012018
Arrive和Depart是字符串。
我尝试使用以下内容转换df2['Arrive']
df2['Arrive'] = pd.to_datetime(df2['Arrive'])
然而,这会引发错误:
TypeError: Unrecognized value type: <class 'str'>
我浏览了很多文章但却找不到出错的地方或如何修复它。
答案 0 :(得分:3)
在to_datetime
中添加errors='coerce'
参数format='%m%d%Y'
:
df2['Arrive'] = pd.to_datetime(df2['Arrive'], errors='coerce', format='%m%d%Y')
print (df2)
date Arrive Depart
0 20180516 NaT NaN
1 20180516 NaT NaN
2 20180518 2018-06-17 6242018
3 20180515 NaT NaN
4 20180519 NaT NaN
5 20180517 NaT NaN
6 20180515 2018-06-05 6062018
7 20180517 2018-08-13 8162018
8 20180515 2018-07-11 7152018
9 20180517 2018-07-27 8012018