0

I Have a column named "Date" which has values of the form '20041230'. How to convert this to 2004-12-30 in pandas.

I tried applying pd.to_datetime to the column, but I am getting garbage values attached to the date.

  • `>>> pd.to_datetime("20041230")` gives `Timestamp('2004-12-30 00:00:00')` for me. Can you show us your output? – Chrysophylaxs Jan 02 '23 at 19:13
  • 1
    What type do you want as output? datetime or string? What do you have as input? String or integer? – mozway Jan 02 '23 at 19:17
  • Does this answer your question? [How to change the datetime format in Pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – FObersteiner Jan 02 '23 at 19:30

1 Answers1

0

A safe method to have strings would be:

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')

For datetime type, use normalize:

df['Date'] = pd.to_datetime(df['Date']).dt.normalize()
mozway
  • 194,879
  • 13
  • 39
  • 75