Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Unrecognized value type: <class 'str'>

Tags:

python

pandas

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.

like image 749
Luke Avatar asked Nov 22 '25 17:11

Luke


1 Answers

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
like image 190
jezrael Avatar answered Nov 24 '25 07:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!