3

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?

1
  • Take a look at .datetime(). The format your strings are currently in might make it more difficult though. Commented Jan 26, 2021 at 20:54

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Pablo! Just pd.to_datetime(df["my_column"]) gave me wished result. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.