Newlearner
Newlearner

Reputation: 33

time data 'Month' does not match format '%Y-%b' (match)

Hi I am working on a data set given below

Month,Travellers('000)
Jan-91,1724
Feb-91,1638
Mar-91,1987
Apr-91,1825
May-91,
Jun-91,1879

I am using the below code to format the date

data = pd.read_csv('Metrail+dataset.csv', header = None)
data.columns = ['Month','Travellers']
data['Month'] = pd.to_datetime(data['Month'], format='%m-%Y')
data = data.set_index('Month')
data.head(12)

However, getting the below error

ValueError: time data 'Month' does not match format '%m-%Y' (match)

Could someone help me what is the mistake and any useful links to learn more on the date format

Upvotes: 0

Views: 300

Answers (2)

azro
azro

Reputation: 54148

  1. %Y is for year on 4 digits < VS > %y is for year on 2 digits

  2. %m is for month with digits < VS > %b is for shorten month name

Also remove header=None because this counts the header row as data, this is wrong

data = pd.read_csv('data.csv')
data.columns = ['Month', 'Travellers']
data['Month'] = pd.to_datetime(data['Month'], format='%b-%y')

Upvotes: 1

Evhz
Evhz

Reputation: 9246

use %b and (as mentioned) %y

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

From the docs

%b Month as locale’s abbreviated name. Sep

Upvotes: 1

Related Questions