1

In my csv file, I have date column as dd/mm/yyyy. However, when I import it into Python 3, it automatically changes the entire column to yyyy/mm/dd and its data type is an object.

I have tried converting it back to dd/mm/yyyy with this:

datetime.datetime.strptime(df_sales_sum['Date'], "%Y/%m/%d").datetime.strftime("%d-%m-%Y")

However, it returned me with this

TypeError: strptime() argument 1 must be str, not Series

Am i supposed to convert the df_sales_sum['Date'] part to a string first? And from what I know, datetime automatically converts it to yyyy/mm/dd. Correct me if im wrong.

Any help on converting it from yyyy/mm/dd to dd/mm/yyyy will be appreciated

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
Jonathan
  • 424
  • 4
  • 14

3 Answers3

3

Yes, I would make df_sales_sum['Date'] a string: str(df_sales_sum['Date']). See if that helps. However, I'm not sure what df_sales_sum['Date'] output is.

df_date = str(df_sales_sum['Date'])

convert_date = df_date.strftime("%d/%m/%Y")

Good luck.

2

I think this should help:

df['Column_name'] = pd.to_datetime(df['Column_name'], format='%d%m%Y')
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
Surya Lohia.
  • 446
  • 4
  • 16
1

The error said that, you should convert df_sales_sum['Date'] to python string first. Then strptime will accept that string to convert to a datetime object.