-1

I would like to convert pandas column that set as a string, to datetime format. How do I convert the column?

  • DataFrame Name: df
  • Column Name: date
  • Value format in column: 2011-06-12T01:17:56
Buzi
  • 248
  • 3
  • 12
  • 1
    Does this answer your question? [Convert Pandas Column to DateTime](https://stackoverflow.com/questions/26763344/convert-pandas-column-to-datetime) – I'mahdi Oct 12 '21 at 12:30
  • 1
    Just `pd.to_datetime(df['date'])` – FObersteiner Oct 12 '21 at 13:05
  • @user1740577 I of course looked at this one before posting this new one. It didn't help with this specific format. That's why I asked this one. – Buzi Oct 13 '21 at 09:09
  • @MrFuppes I didn't know it work without a specific format. Thank you! I sure feel a little dumb now. – Buzi Oct 13 '21 at 09:11
  • 1
    no worries ^^ afaik, internally, pd.to_datetime falls back to [dateutil's parser](https://dateutil.readthedocs.io/en/stable/parser.html), which works quite nicely. – FObersteiner Oct 13 '21 at 10:19

2 Answers2

3
df['datetime'] = pd.to_datetime(df['date'].astype(str) + ' ' + df['time'].astype(str))


df['datetime'] = pd.to_datetime(df['datetime'], format= '%d/%m/%Y %H:%M:%S.%f') 
rafine
  • 361
  • 3
  • 18
1

You can use pd.to_datetime for this.

import pandas as pd
df = pd.DataFrame({"date": ["2011-06-12T01:17:56"]})

Conversion using map method:

df["date"].map(pd.to_datetime)

or

Conversion using apply method:

df["date"].apply(pd.to_datetime)

or

Conversion using function on column series:

df["date"] = pd.to_datetime(df["date"])
Dascienz
  • 1,071
  • 9
  • 14