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

Assistance converting yyyy-mm-ddThh:mm:ss.000+0000 in Pandas

I am working with some Salesforce data in python/pandas library and was wondering if their was a clean way of converting this string to a date time, or even just a date in a specific column.

Here is a sample of what the format looks like.

enter image description here

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

I am just trying to convert these string fields into either a date or datetime. Either or is fine.

Thanks for any assistance.

Note I found examples of date conversion but none without the +0000 at the very end or with the T between the date and timestamp.

I am able to individually remove pieces through a series of variables, but I know there has to be a better way.

For instance.

result[‘CreatedDate’] = result[‘CreatedDate’].str.replace(‘T’, ‘ ‘) But then of course I have to do another pass on the + and the other pieces of the string.

>Solution :

Use to_datetime():

import pandas as pd


result = pd.DataFrame(data={"CreatedDate": ["2023-07-14T21:33:29.000+0000"]})

result["CreatedDate"] = pd.to_datetime(arg=result["CreatedDate"], utc=True).dt.date
print(result)
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