3
mydata = [{'datetime' : '2015-09-01 00:00:23.205', 'Timezoneoffset': -5},
      {'datetime' : '2015-09-01 00:41:27.659', 'Timezoneoffset': -6},
      {'datetime' : '2015-09-01 00:13:40.897', 'Timezoneoffset': -1},
      {'datetime' : '2015-09-02 00:42:06.007', 'Timezoneoffset': 5}]
t = pd.DataFrame(mydata)
t.datetime = pd.to_datetime(t.datetime,format='%Y-%m-%d %H:%M:%S.%f')

I would like to create third column which should look like this:

2015-08-31 19:00:23.205
2015-08-31 18:41:27.659
2015-08-31 23:13:40.897
2015-09-01 05:42:06.007

Any help appreciated. Thank you!

2 Answers 2

3

Construct a TimedeltaIndex and add it to the existing datetime column:

In [241]:
t['datetime_off'] = t['datetime'] + pd.TimedeltaIndex(t['Timezoneoffset'], unit='h')
t

Out[241]:
   Timezoneoffset                datetime            datetime_off
0              -5 2015-09-01 00:00:23.205 2015-08-31 19:00:23.205
1              -6 2015-09-01 00:41:27.659 2015-08-31 18:41:27.659
2              -1 2015-09-01 00:13:40.897 2015-08-31 23:13:40.897
3               5 2015-09-02 00:42:06.007 2015-09-02 05:42:06.007
Sign up to request clarification or add additional context in comments.

Comments

1

You could convert the type of Timezoneoffset to timedelta64[h] (hours) and then add them

In [105]: t['datetime'] + t['Timezoneoffset'].astype('timedelta64[h]')
Out[105]:
0   2015-08-31 19:00:23.205
1   2015-08-31 18:41:27.659
2   2015-08-31 23:13:40.897
3   2015-09-02 05:42:06.007
dtype: datetime64[ns]

Comments

Your Answer

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.