0

Hello all pandas experts. I am trying to remove time zone info string from a pandas column. I just need the date and time. Here's what I did:

from datetime import datetime

df['created_at'] = pd.to_datetime(df['created_at']).dt.strftime('%Y-%m-%d H%:M%:S%')

Instead, I got this error msg.

ParserError: Unknown string format: 2021-08-01 22:47:19 Malay Peninsula Standard Time

Thanks in advance.

  • pls feel free to comment if you think that the question is wrongly closed – Anurag Dabas Aug 22 '21 at 06:02
  • Tried also this but still getting error. `df['created_at'] = df['created_at'].dt.tz_localize(None)` Error msg `AttributeError: Can only use .dt accessor with datetimelike values` – kungfupyndas Aug 22 '21 at 06:08
  • convert to datetime first `pd.to_datetime(df['created_at']).dt.tz_localize(None)` – Anurag Dabas Aug 22 '21 at 06:08
  • Got this error. `ParserError: Unknown string format: 2021-08-01 22:47:19 Malay Peninsula Standard Time`. Need to strip out the timezone string first. – kungfupyndas Aug 22 '21 at 06:09
  • pass `errors='coerce'` in `to_datetime()` method `pd.to_datetime(df['created_at'],error='corece').dt.tz_localize(None)` if you again get any error then updated your question with a sample dataframe and your expected output from it – Anurag Dabas Aug 22 '21 at 06:11
  • 2
    Finally found the solution. `df["created_at"] = [re.sub(r'[^0-9-:\s+]','', created_at.rstrip()) for created_at in df['created_at']]` and then `df["created_at"] = pd.to_datetime(df['created_at'])` Thanks anyways. – kungfupyndas Aug 22 '21 at 06:17

0 Answers0