0

I have a dataframe that looks like that:

   conversation__created_at
0  2020-10-15T03:39:42.766773+00:00
1  2020-10-14T11:24:33.831177+00:00
2  2020-10-14T08:29:44.192258+00:00
3  2020-10-14T01:42:06.674313+00:00
4  2020-10-13T12:57:04.218184+00:00

How to convert it into GMT +7?

Wasif
  • 14,755
  • 3
  • 14
  • 34
ab.trubex
  • 3
  • 1
  • 2

4 Answers4

2

I assume you have a pandas series because the data you posted looks like one. Then you can use tz_convert, i.e.

import pandas as pd
pd.to_datetime('2020-10-15T03:39:42.766773+00:00').tz_convert('Etc/GMT+7')

As pointed out in the comments, since the datetime carries a T in it, it is of string format, thus we need to convert to datetime first and then convert to the correct timezone.

pd.to_datetime(series).dt.tz_convert('Etc/GMT+7')
Stefan
  • 1,697
  • 15
  • 31
  • 1
    the OP's input is of dtype string since there still is the 'T' separator between date and time, so pd.to_datetime presumably is required ;-) – FObersteiner Oct 26 '20 at 07:34
  • @MrFuppes, thanks for pointing that out. I never noticed that `T` is actually indicating string fromat. I updated my answer. – Stefan Oct 26 '20 at 07:36
  • oh and there's one more caveat; `'Etc/GMT+7'` is UTC-7 (not +7). not sure what the OP expects here. would be easier if a certain time zone was named. – FObersteiner Oct 26 '20 at 07:51
  • so what's the syntax for +7 (Jakarta)? – ab.trubex Oct 26 '20 at 08:27
  • @ab.trubex There is the timezone `Asia/Jakarta`, so you can use this one. – Stefan Oct 26 '20 at 08:40
2

You can use datetime library only.

from datetime import datetime, timedelta, timezone

d = datetime.fromisoformat("2020-10-15T03:39:42.766773+00:00")
tz = timezone(timedelta(hours=7))
new_time = d.astimezone(tz)
HK boy
  • 1,398
  • 11
  • 17
  • 25
0

you can use pytz to set timezone for your datetime instance

for example:

from pytz import timezone
from datetime import datetime

date = datetime.now()

print(date)
tz = timezone("Etc/GMT+7")
date = date.replace(tzinfo=tz)
print(date)

out put:

2020-10-26 10:33:25.934699
2020-10-26 10:33:25.934699-07:00
0

You can apply pytz.timezone on the df

from pytz import timezone
from datetime import datetime

def myDate(x): 
  tz = timezone("Etc/GMT+7")
  dt = x.replace(tzinfo=tz)
  return dt

df['conversation__created_at'] = df.apply(lambda row: myDate(row['conversation__created_at'].to_pydatetime()))
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • once more things, i add df = pd.read_csv('filename.csv') – ab.trubex Oct 26 '20 at 08:03
  • besides that there is a pandas built-in for this task, I think you shouldn't suggest to use `replace` here. Although this works for static offsets, it causes wrong offsets (LMT) for actual time zones, see [pytz localize vs. replace](https://stackoverflow.com/q/1379740/10197418). – FObersteiner Oct 26 '20 at 09:18