1

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'

2
  • pd.to_datetime(df['date'], utc=True)? Commented Mar 9, 2020 at 13:46
  • After that, I get an error: Unknown string format: 1979-01-01 00:00:00 +0000 UTC Commented Mar 9, 2020 at 13:48

2 Answers 2

3

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') 
Sign up to request clarification or add additional context in comments.

1 Comment

It worked, I changed it to: df.date = df.date.astype(str) df['date'] = df.date.str[:-3] and then df['date'] = pd.to_datetime(df['date'], utc=True) Thank you
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.