harp1814
harp1814

Reputation: 1658

Pandas: Convert String column to timestamp

I have Pandas dataframe with string column which contains timestamp as "20000530 172700" How to change elegantly such string to "2000-05-30 17:27:00" ? Dataframe contains > 10k rows. I don't want take each value,insert "-" and ":" to specified positions. Is there a solution using mask?

Upvotes: 3

Views: 4691

Answers (1)

Pablo C
Pablo C

Reputation: 4761

you can use pandas.to_datetime:

pd.to_datetime(df["my_column"])

If you want to customize it, you can use pandas.Series.dt.strftime, e.g.:

pd.to_datetime(df["my_column"]).dt.strftime('%d%b%Y')
#The format will be something like 30May2000

You can check all the datetime format codes here.

Upvotes: 6

Related Questions