3

I am trying to format some dates with datetime, but for some reason it is ignoring my format call. I want day/month/Year format which is what the CSV file has the format is, but when I try this.

df = pd.read_csv('test.csv', parse_dates=['Date'],
             date_parser=lambda x: pd.to_datetime(x, format='%d/%m/%Y'))

Result:

enter image description here

Why is it what I can only assume "defaulting" to %Y-%m-%d ???

4
  • 3
    The format specifies what your input data looks like. Once it’s converted to a datetime it will be displayed YYYY-MM-DD HH:mm:SS. You can change the display with strftime, but it’s probably best to leave it as a datetime to perform calculations Commented Oct 14, 2018 at 1:11
  • Can you specify the format of the Date column in test.csv which you are reading or please add a screenshot of the csv file. The issue is probably because of the month and day parameter interchanged. Commented Oct 14, 2018 at 1:37
  • The CSV format is D/M/Y. Commented Oct 14, 2018 at 1:45
  • Csv here: drive.google.com/open?id=1NyOUVQzNxUl-dwVPuwXiBAIHiGsMnmao Commented Oct 14, 2018 at 1:59

1 Answer 1

2

This should work.

import datetime as dt
import pandas as pd

df = pd.read_csv('test.csv')

formatted_dates =[]

for old_date in df['Date']:

    dt_obj = dt.datetime.strptime(old_date,'%d/%m/%Y')

    new_date = """{}/{}/{}""".format(dt_obj.day,dt_obj.month,dt_obj.year)

    formatted_dates.append(new_date)

df['Date'] = formatted_dates

Output:

18/1/2017
22/1/2017
31/1/2017
...

P.S. There's a bug with the parse_dates,date_parser in pd.read_csv which automatically changes the format to the YYYY-MM-DD.

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

7 Comments

thanks for replying, I'm getting an error with that code <lambda> date_parser=lambda x: dt.datetime.strptime(x, format='%d/%m/%Y')) TypeError: strptime() takes no keyword arguments
@user3170725 can you share the csv through google drive, it would be helpful. Thanks!
Strange! Tried lots of different methods still the result is same as yours. Will come back to you.
Thanks! Yes very annoying. I can convert it to a string and it works, but doesn't work well for plotting.
|

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.