ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

processing timestamps in different formats in Pandas

Consider this simple example

import pandas as pd

mydf = pd.DataFrame({'timestamp': ['Tue, 27 Jul 2021 06:43:18 +0000',
                            'Sun, 20 Jun 2021 17:00:17 GMT',
                            'Wed, 28 Jul 2021 08:44:00 -0400']})

mydf
Out[50]: 
                         timestamp
0  Tue, 27 Jul 2021 06:43:18 +0000
1    Sun, 20 Jun 2021 17:00:17 GMT
2  Wed, 28 Jul 2021 08:44:00 -0400

I am trying to convert all the timestamps to GMT and get rid of the timezone offset. Unfortunately, the usual solution does not work

pd.to_datetime(mydf['timestamp']).dt.tz_localize(None)
AttributeError: Can only use .dt accessor with datetimelike values

What is the issue? Thanks!

Upvotes: 1

Views: 110

Answers (1)

kkgarg
kkgarg

Reputation: 1376

The problem is your function call is not able to convert the mydf['timestamp'] into datetime object but plain object and therefore, when you try to access .dt, it complains.

You can use the flag utc=True to do time-zone aware conversion.

pd.to_datetime(mydf['timestamp'], utc=True).dt.tz_localize(None)

Upvotes: 3

Related Questions