1

datatime available as object

enter image description here

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'

output data type enter image description here

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • 5
    It's best to [not use images for error messages](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). The text will be of help for those that use assistive technologies and also allows for your error message to be indexed by search engines, possibly helping others in the future. – Jason Feb 24 '21 at 14:05
  • I suspect you neither need `apply` nor provide a `format` to pd.to_datetime. Then, have a look at https://stackoverflow.com/q/16628819/10197418 – FObersteiner Feb 24 '21 at 14:13

1 Answers1

0

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
Patrick FitzGerald
  • 3,280
  • 2
  • 18
  • 30