1

I have a dataframe that looks like that:

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

How to convert it into GMT +7?

avarice
15.7k3 gold badges19 silver badges38 bronze badges
asked Oct 26, 2020 at 6:59
0

4 Answers 4

3

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')
answered Oct 26, 2020 at 7:06
Sign up to request clarification or add additional context in comments.

5 Comments

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 ;-)
@MrFuppes, thanks for pointing that out. I never noticed that T is actually indicating string fromat. I updated my answer.
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.
so what's the syntax for +7 (Jakarta)?
@ab.trubex There is the timezone Asia/Jakarta, so you can use this one.
2

You can use datetime library only.

from datetime import datetime, timedelta, timezone
d = datetime.fromisoformat("2020年10月15日T03:39:42.766773+00:00")
tz = timezone(timedelta(hours=7))
new_time = d.astimezone(tz)
answered Oct 26, 2020 at 7:20

2 Comments

how to collaborate it in pandas? do we need for loop?
With a list of time, you can use for loop. This snippet doesn't need pandas.
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
answered Oct 26, 2020 at 7:05

Comments

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()))
answered Oct 26, 2020 at 7:13

2 Comments

once more things, i add df = pd.read_csv('filename.csv')
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.