Vinyl Warmth
Vinyl Warmth

Reputation: 2516

Why are dates in pandas being displayed in a different format to input (CSV) data?

I have a CSV which has dates in this format:

Date
01/01/1997
02/01/1997
03/01/1997
04/01/1997

I am importing the data into a dataset using df = pd.read_csv('data.csv')

When I look at the data held in the dataframe it appears in a different format:

df['Date']


Date
1997-1-1
1997-1-2
1997-1-3
1997-1-4

I don't understand why this is happening.

I've tried Googling & looking on SOF but haven't been able to find the answer...

Upvotes: 0

Views: 118

Answers (2)

Hari_pb
Hari_pb

Reputation: 7416

You can use parse_dates option from read_csv to get data in same format as on your .csv file. Key is dayfirst=True to get dates first then month and you can change accordingly. You can also change the format as below:

df.apply(pd.to_datetime, dayfirst=True)

For further readings, refer the documentation http://pandas-docs.github.io/pandas-docs-travis/

Upvotes: 1

DeepSpace
DeepSpace

Reputation: 81614

Because that's pandas default time format.

You can pass read_csv dayfirst=True, as can be seen in the documentation:

dayfirst : boolean, default False DD/MM format dates, international and European format

Upvotes: 1

Related Questions