简体   繁体   中英

Converting column in Dataframe to datetime

I've been trying to use the to_datetime function to convert values in my column to datetime:

df['date'] = pd.to_datetime(df['date'],errors='coerce',format='%Y-%m-%d %H:%M:%S %z %Z') 

After that, I received only NaT values.

Example: Value Format in Column: '1979-01-01 00:00:00 +0000 UTC'

I think you can't parse utc offset (+0000) and timeszone information at the sime time.

You might want to remove the UTC at the end and only parse the offset.

df['date'] = df.date.str[:-4]
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S %z') 

Pandas can't manage both %z and %Z as you can see here . Note that Python's strptime can handle this, but doesn't deal with %Z .

In your case you might want to just peel off the last bit with ser.str and consider opening a feature request.

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