Mullins
Mullins

Reputation: 77

Issue converting python dataframe datatype from object to datetime

An example of a value in the Date column:

19/08/2017

Previously, I tried:

dividends['Date'] = pd.to_datetime(dividends['Date'])

to convert my column to have a datetime date type. However, when I then created charts out of this dataset (with 'Date' having a datetime date type), it always looked odd:

enter image description here

Having done a bit of trouble shooting online, I think the reason is because of the formatting of my datetime conversion. However, when I try to use this:

dividends['Date'] = pd.to_datetime(dividends['Date'],format='%d-%m-%y')

I get the error message

ValueError: time data '19/08/2017' does not match format '%d-%m-%y' (match)

Why is this, and how do I fix it? Cheers.

Upvotes: 1

Views: 33

Answers (1)

oreopot
oreopot

Reputation: 3450

Use the following code:(note that I used Y in format):

dividends['Date'] = pd.to_datetime(dividends['Date'],format='%d-%m-%Y')

You could also use the following format:

'%d/%m/%Y'

Upvotes: 1

Related Questions