Reputation: 213
I am currently trying to convert a Pandas column into a datetime column in order to work out the differences between three sets of date columns (1. date of hotel search, 2. date of stay arrival, 3. date of departure)
Here is a sample of the how it looks:
>>> 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
The Arrive and Depart are strings.
I tried to convert df2['Arrive']
by using:
df2['Arrive'] = pd.to_datetime(df2['Arrive'])
However this throws an error:
TypeError: Unrecognized value type: <class 'str'>
I went through many articles but couldn't quite find what was going wrong or how to fix it.
Upvotes: 21
Views: 28375
Reputation: 862406
Add parameter errors='coerce'
with format='%m%d%Y'
in to_datetime
:
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
Upvotes: 28