Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: most efficient way to extract timestamp from a string

I have a column that I want to convert from a string to a date time timestamp. Each row in the respective column contains data as a string in this format: "01.01.2020 00:00 – 01.01.2020 00:15". I want to convert it to a date time object "2020-01-01 00:00:00+00:00"

I just need the first part of the date and the excess is unnecessary. I know I could split it in multiple columns on space as a delimiter and use:

    pd.to_datetime(df['Date/time before conversion'].format='%d.%m.%Y %H:%M')

But is there a more efficient manner where I can directly assign on the string which bit is year, month etc. while skipping the fluff I don’t need?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use:

pd.to_datetime(df['Date/time before conversion'].str.split().str[0], dayfirst=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading