Search code examples
pythonpandasdatetimesalesforce

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

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)