Reputation: 33
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
Reputation: 54148
%Y
is for year on 4 digits < VS > %y
is for year on 2 digits
%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