Home > Software engineering >  Convert Pandas column to datetime for a specific datetime format
Convert Pandas column to datetime for a specific datetime format

Time:10-13

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

CodePudding user response:

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') 

CodePudding user response:

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"])
  • Related