0

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...

6
  • What does df["Date"].dtype show? I believe pandas has automatically recognized that this column contains dates and converted them to datetype objects. You may wish to keep df["Date"] in this format, however, and do what @DeepSpace suggested below. Or add dtype={"Date" : str} to your read_csv() call to keep these values unchanged (and as strings). Commented Jul 27, 2016 at 14:55
  • 4
    YYYY-MM-DD is the ISO 8601 date format. iso.org/iso/home/standards/iso8601.htm Commented Jul 27, 2016 at 14:56
  • @dmn df['Date'].dtype gives dtype('O') ?? If I pass dtype={'GMT': str} I still get dtype('O') Commented Jul 27, 2016 at 15:06
  • @VinylWarmth that's ok. When you pass in dtype={"GMT" : str} do the dates look the way you want them to? Commented Jul 27, 2016 at 15:25
  • 1
    dtype('O') - means 'object' (or string in pandas terminology) Commented Jul 27, 2016 at 15:59

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Can you explain why the same isn't happening for another dataset? In the other dataset the CSV looks like 12/02/2006 and in pandas it looks the same: 12/02/2006
@VinylWarmth Try what @dmn suggested in the comments on both the datasets. It is possible that in the second case the column type is str and not datetime.
1

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/

1 Comment

@VinylWarmth please verify the answer if this worked for you .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.