Joe
Joe

Reputation: 59

Convert string to time in Pandas

2019-02-28 06:15 +08:00

I am trying to convert this string to a date. It should equal 2019-02-27 22:15 as I want to subtract the 8 hours. How would I go about this?

Upvotes: 3

Views: 324

Answers (3)

jezrael
jezrael

Reputation: 862641

With the tz=None, we can remove the timezone (after converting to UTC if necessary) with Timestamp.tz_convert, if necessary convert values to datetimes:

a = '2019-02-28 06:15 +08:00'
b = pd.to_datetime(a).tz_convert(None)
print (b)
2019-02-27 22:15:00

If column in DataFrame use Series.dt.tz_convert:

df = pd.DataFrame({'date':['2019-02-28 06:15 +08:00','2019-02-28 06:15 +08:00']})

df['date'] = pd.to_datetime(df['date']).dt.tz_convert(None)
print (df)
                 date
0 2019-02-27 22:15:00
1 2019-02-27 22:15:00

Upvotes: 2

cristian hantig
cristian hantig

Reputation: 1310

you can try this

import pandas as pd
d = '2019-02-28 06:15 +08:00'
pd.to_datetime(d).tz_convert('UTC')

Timestamp('2019-02-27 22:15:00+0000', tz='UTC')

Upvotes: 1

Reznik
Reznik

Reputation: 2806

You can do this:

import pandas as pd
pd.Timestamp('2019-02-28 06:15 +08:00').tz_convert("UTC") # Timestamp('2019-02-27 22:15:00+0000', tz='UTC')

Upvotes: 2

Related Questions