Home > Software design >  pandas.to_datetime -> ValueError: unconverted data remains: 01
pandas.to_datetime -> ValueError: unconverted data remains: 01

Time:10-27

I am trying to convert the str column below:

data['date']

0          Apr-2001
1          Jun-1987
2          Apr-2011
3          Feb-2006
4          Dec-2000
             ...   
2260663    Jan-2004
2260664    Mar-2003
2260665    Feb-1995
2260666    Jul-2003
2260667    Apr-2014
Name: earliest_cr_line, Length: 2260668, dtype: object

to a datatime column :

data['datetime'] = pd.to_datetime(data['date'], format = '%b-%y')

which gives me the following error msg

ValueError: unconverted data remains: 01

Could someone please advise what is wrong here?

Thanks.

CodePudding user response:

Use Y for years in format YYYY:

data['datetime'] = pd.to_datetime(data['date'], format = '%b-%Y')

Like @MrFuppes mentioned, also working:

data['datetime'] = pd.to_datetime(data['date'])

If need format without days is possible use monthly periods:

data['per'] = pd.to_datetime(data['date']).dt.to_period('m')
  • Related