datatime available as object
converting to datetime type
df[['x','y']] = df[['x','y']].apply(pd.to_datetime, format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
here I want to exclude time zone information 'UTC'
datatime available as object
converting to datetime type
df[['x','y']] = df[['x','y']].apply(pd.to_datetime, format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
here I want to exclude time zone information 'UTC'
From your code sample, I see that you are wanting to process multiple variables at once. In this case, you can exclude the time zone information by using a lambda function to apply the pd.to_datetime
function and extract the time zone naive timestamps with the values
property. Here is an example:
import pandas as pd # v 1.1.3
df = pd.DataFrame(dict(x=['2020-12-30 12:00:00 UTC', '2020-12-30 13:00:00 UTC',
'2020-12-30 14:00:00 UTC', '2020-12-30 15:00:00 UTC',
'2020-12-30 16:00:00 UTC'],
y=['2020-12-31 01:00:00 UTC', '2020-12-31 04:00:00 UTC',
'2020-12-31 02:00:00 UTC', '2020-12-31 05:00:00 UTC',
'2020-12-31 03:00:00 UTC']))
df[['x','y']] = df[['x','y']].apply(lambda x: pd.to_datetime(x).values)
print(df[['x','y']])
# x y
# 0 2020-12-30 12:00:00 2020-12-31 01:00:00
# 1 2020-12-30 13:00:00 2020-12-31 04:00:00
# 2 2020-12-30 14:00:00 2020-12-31 02:00:00
# 3 2020-12-30 15:00:00 2020-12-31 05:00:00
# 4 2020-12-30 16:00:00 2020-12-31 03:00:00