-1

I have a dataframe with a column called date, formatted as string and it looks like this:

date
20200109
20200304
20210112

Is there a way I can format that into datetime?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
A.N.
  • 580
  • 4
  • 15
  • 2
    Duplicate of [How to convert string to datetime format in pandas python?](https://stackoverflow.com/questions/32204631/how-to-convert-string-to-datetime-format-in-pandas-python) – mkrieger1 Aug 22 '22 at 22:53

1 Answers1

3

You can use pd.to_datetime with custom format=:

df["date"] = pd.to_datetime(df["date"], format="%Y%d%m")  # use "%Y%m%d" if month is first
print(df)

Prints:

        date
0 2020-09-01
1 2020-04-03
2 2021-12-01

Note: datetime format cheatsheet

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91