Changing timezone on pandas DatetimeIndex when concatenating DataFrames in Python

霸气de小男生 提交于 2019-12-11 08:34:45

问题


I'm on pandas 0.14.1. Have two DataFrames both indexed by timezone-aware DatetimeIndex:

import pandas as pd
ix1 = pd.DatetimeIndex(start=pd.Timestamp('20140715', tz='EST5EDT'), end=pd.Timestamp('20140717', tz='EST5EDT'), freq='D', tz='EST5EDT')
ix2 = pd.DatetimeIndex([pd.Timestamp('2014-07-11 00:00:00', tz='EST5EDT'), pd.Timestamp('2014-07-21 00:00:00', tz='EST5EDT')])
df1 = pd.DataFrame(0, index=ix1, columns=['A', 'B'])
df2 = pd.DataFrame(0, index=ix2, columns=['A', 'B'])

Both indices having timezone, one with freq and one without:

df1.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-15 00:00:00-04:00, ..., 2014-07-17 00:00:00-04:00]
Length: 3, Freq: D, Timezone: EST5EDT


df2.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-11 00:00:00-04:00, 2014-07-21 00:00:00-04:00]
Length: 2, Freq: None, Timezone: EST5EDT

Then concatenating the two changes timezone to UTC:

pd.concat([df1, df2]).index
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-15 04:00:00+00:00, ..., 2014-07-21 04:00:00+00:00]
Length: 5, Freq: None, Timezone: UTC

I wonder if this is a known bug or it has a particular reason.

来源:https://stackoverflow.com/questions/24830952/changing-timezone-on-pandas-datetimeindex-when-concatenating-dataframes-in-pytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!