Search code examples
pythondatetimepandaspytz

Timestamps with different timezones in the same pandas DatetimeIndex object?


Is it possible to convert a pd.DatetimeIndex consisting of timestamps in a single timezone to one where each timestamp has its own, in some cases distinct timezone?

Here is an example of what I would like to have:

type(df.index)

 pandas.tseries.index.DatetimeIndex

df.index[0]

Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London')

df.index[1]

Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels') 

Solution

  • You can have an index contain Timestamps with different timezones. But you would have to explicity construct it as an Index.

    In [33]: pd.Index([pd.Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London'),pd.Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels')],dtype='object')
    Out[33]: Index([2015-06-07 23:00:00+01:00, 2015-06-08 00:01:00+02:00], dtype='object')
    
    In [34]: list(pd.Index([pd.Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London'),pd.Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels')],dtype='object'))  
    Out[34]: 
    [Timestamp('2015-06-07 23:00:00+0100', tz='Europe/London'),
     Timestamp('2015-06-08 00:01:00+0200', tz='Europe/Brussels')]
    

    This is a very odd thing to do, and completely non-performant. You generally want to have a single timezone represented (UTC or other). In 0.17.0, you can represent efficiently a single column with a timezone, so one way of accomplishing what I think your goal is to segregate the different timezones into different columns. See the docs