简体   繁体   中英

How can I parse this date format into datetime? Python/Pandas

The starting date format I currently have is 2019-09-04 16:00 UTC+3 and I'm trying to convert it into a datetime format of 2019-09-04 16:00:00+0300 .

The format I thought would work was format='%Y-%m-%d %H:%M %Z%z' , but when I run it I get the error message ValueError: Cannot parse both %Z and %z .

Does anyone know the correct format to use, or should I be trying a different method altogether? Thanks.

Edit Sorry, I had a hard time putting into words what it is I am looking to do, hopefully I can clarify.

I'm looking to change all the date and times in a dataframe into the datetime format.

This is the method I was trying to use which presented me with an error df['datepicker'] = pd.to_datetime(df['datepicker'], format='%Y-%m-%d %H:%M %Z%z')

And here is a sample of the data I currently have.

datepicker
2019-09-07 16:00 UTC+2
2019-09-04 18:30 UTC+4
2019-09-06 17:00 UTC±0
2019-09-10 16:00 UTC+1
2019-09-04 18:00 UTC+3

And this is what I'm looking to convert them into, a timestamp format.

datepicker
2019-09-07 16:00:00+0200
2019-09-04 18:30:00+0400
2019-09-06 17:00:00+0000
2019-09-10 16:00:00+0100
2019-09-04 18:00:00+0300

When i defined as below. it works as you expect.

from datetime import datetime, timedelta, timezone

UTC = timezone(timedelta(hours=+3))
dt = datetime(2019, 1, 1, 12, 0, 0, tzinfo=UTC)
timestampStr = dt.strftime("%Y-%m-%d %H:%M %Z%z")
print(timestampStr)

With the output of:

2019-01-01 12:00 UTC+03:00+0300

pandas.to_datetime should parse this happily if you tweak the strings slightly:

import pandas as pd
df = pd.DataFrame({"datepicker":[ "2019-09-07 16:00 UTC+2", "2019-09-04 18:30 UTC+4",
                                  "2019-09-06 17:00 UTC±0", "2019-09-10 16:00 UTC+1", 
                                  "2019-09-04 18:00 UTC+3"]})
df['datetime'] = pd.to_datetime(df['datepicker'].str.replace('±', '+'))
# df['datetime']
# 0    2019-09-07 16:00:00-02:00
# 1    2019-09-04 18:30:00-04:00
# 2    2019-09-06 17:00:00+00:00
# 3    2019-09-10 16:00:00-01:00
# 4    2019-09-04 18:00:00-03:00
# Name: datetime, dtype: object

Note that due to the mixed UTC offsets, the column's data type is 'object' (datetime objects). If you wish, you can also convert to UTC straight away, to get a column of dtype datetime[ns]:

df['UTC'] = pd.to_datetime(df['datepicker'].str.replace('±', '+'), utc=True)
# df['UTC']
# 0   2019-09-07 18:00:00+00:00
# 1   2019-09-04 22:30:00+00:00
# 2   2019-09-06 17:00:00+00:00
# 3   2019-09-10 17:00:00+00:00
# 4   2019-09-04 21:00:00+00:00
# Name: UTC, dtype: datetime64[ns, UTC]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM