How to set timestamp offset in pandas framework?

I have a DataFrame with two columns. One column is filled with timestamps, the other column contains the UTC offset in hours of the timestamp in the same row.

DataFrame is as follows:

In [44]: df Out[44]: DATETIME OFFSET 0 2013-01-01 00:00:00+00:00 1 1 2013-01-01 01:00:00+00:00 1 2 2013-01-01 02:00:00+00:00 1 3 2013-01-01 03:00:00+00:00 1 4 2013-01-01 04:00:00+00:00 1 5 2013-01-01 05:00:00+00:00 1 6 2013-01-01 06:00:00+00:00 2 7 2013-01-01 07:00:00+00:00 2 8 2013-01-01 08:00:00+00:00 2 

What I like is to add the offset per line to the timestamp:

 In [44]: df Out[44]: DATETIME OFFSET 0 2013-01-01 00:00:00+01:00 1 1 2013-01-01 01:00:00+01:00 1 2 2013-01-01 02:00:00+01:00 1 3 2013-01-01 03:00:00+01:00 1 4 2013-01-01 04:00:00+01:00 1 5 2013-01-01 05:00:00+01:00 1 6 2013-01-01 06:00:00+02:00 2 7 2013-01-01 07:00:00+02:00 2 8 2013-01-01 08:00:00+02:00 2 

I tried replacing tzinfo but could not find the correct solution. I am thinking of something like the following (pseudocode):

 df.apply(lambda x: x['DATETIME'].replace(tzinfo=pytz.utc + x['OFFSET']) 

Any help is appreciated.

Thank you Thomas

+6
source share
1 answer

It seems that pytz.FixedOffset is suitable for this purpose.

 In [39]: df.apply(lambda x: pd.Timestamp(x['DATETIME'], tz=pytz.FixedOffset(60*x['OFFSET'])), axis=1) Out[39]: 0 2013-01-01 00:00:00+01:00 1 2013-01-01 01:00:00+01:00 2 2013-01-01 02:00:00+01:00 3 2013-01-01 03:00:00+01:00 4 2013-01-01 04:00:00+01:00 5 2013-01-01 05:00:00+01:00 6 2013-01-01 06:00:00+02:00 7 2013-01-01 07:00:00+02:00 8 2013-01-01 08:00:00+02:00 dtype: object 

Others around here use time series more than I do, so this may not be the best practice.

+6
source

Source: https://habr.com/ru/post/947672/


All Articles