close

[Solved] Pandas to_datetime Value: Unknown string format

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Pandas to_datetime Value: Unknown string format in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Pandas to_datetime Value: Unknown string format Error Occurs?

Today I get the following error Pandas to_datetime Value: Unknown string format in python.

How To Solve Pandas to_datetime Value: Unknown string format Error ?

  1. How To Solve Pandas to_datetime Value: Unknown string format Error ?

    To Solve Pandas to_datetime Value: Unknown string format Error I think the problem is in data – a problematic string exists. So you can try check length of the string in column Start Date:

  2. Pandas to_datetime Value: Unknown string format

    To Solve Pandas to_datetime Value: Unknown string format Error I think the problem is in data – a problematic string exists. So you can try check length of the string in column Start Date:

Solution 1

I think the problem is in data – a problematic string exists. So you can try check length of the string in column Start Date:

import pandas as pd
import io

temp=u"""Start Date
1/7/13
1/7/1
1/7/13 12 17
16/7/13
16/7/13"""

data = pd.read_csv(io.StringIO(temp), sep=";", parse_dates=False)

#data['Start Date']= pd.to_datetime(data['Start Date'],dayfirst=True)
print data

     Start Date
0        1/7/13
1         1/7/1
2  1/7/13 12 17
3       16/7/13
4       16/7/13

#check, if length is more as 7
print data[data['Start Date'].str.len() > 7]

     Start Date
2  1/7/13 12 17

Or you can try to find these problematic row different way e.g. read only part of the datetime and check parsing datetime:

#read first 3 rows
data= data.iloc[:3]

data['Start Date']= pd.to_datetime(data['Start Date'],dayfirst=True)

But this is only tips.

EDIT:

Thanks joris for suggestion add parameter errors ='coerce' to to_datetime:

temp=u"""Start Date
1/7/13
1/7/1
1/7/13 12 17
16/7/13
16/7/13 12 04"""

data = pd.read_csv(io.StringIO(temp), sep=";")
#add parameter errors coerce
data['Start Date']= pd.to_datetime(data['Start Date'], dayfirst=True, errors='coerce')
print data

  Start Date
0 2013-07-01
1 2001-07-01
2        NaT
3 2013-07-16
4        NaT

#index of data with null - NaT to variable idx
idx = data[data['Start Date'].isnull()].index
print idx

Int64Index([2, 4], dtype='int64')

#read csv again
data = pd.read_csv(io.StringIO(temp), sep=";")

#find problematic rows, where datetime is not parsed
print data.iloc[idx]

      Start Date
2   1/7/13 12 17
4  16/7/13 12 04

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read