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'

Shaharia Azam
  • 1,948
  • 19
  • 25
Katarzyna
  • 13
  • 2

2 Answers2

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') 
mjspier
  • 6,386
  • 5
  • 33
  • 43
  • 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 – Katarzyna Mar 09 '20 at 14:22
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.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75