2016-09-08 5 views
3

私はtz対応タイムスタンプの列を持つPandas DataFrameを持っており、groupby(level = 0).first()を試しました。私は間違った結果を得る。私は何かを欠いているのか、それともパンダのバグですか?Pandas groupby datatime index、可能なバグ

x = pd.DataFrame(index = [1,1,2,2,2], data = pd.date_range("7:00", "9:00", freq="30min", tz = 'US/Eastern')) 

In [58]: x 
Out[58]: 


    0 
1 2016-09-08 07:00:00-04:00 
1 2016-09-08 07:30:00-04:00 
2 2016-09-08 08:00:00-04:00 
2 2016-09-08 08:30:00-04:00 
2 2016-09-08 09:00:00-04:00 

In [59]: x.groupby(level=0).first() 
Out[59]: 
          0 
1 2016-09-08 11:00:00-04:00 
2 2016-09-08 12:00:00-04:00 
+2

それはバグのように見えます...パンダはUTCにタイムスタンプを変換しますが、それは古いTZ情報を保存しています... – MaxU

+0

間違いなくバグです。 – piRSquared

答えて

2

私はそれがバグだとは思わない。 pytz文書を見ると、US/Easternタイムゾーンの場合、夏時間終了の前後に指定する方法がないことが明確に示されています。

このような場合、UTCを貼り付けるのが最善の選択肢です。 docsから

抜粋次のよう

Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not 
necessarily equal across timezone versions. So if data is localized to 
a specific timezone in the HDFStore using one version of a timezone 
library and that data is updated with another version, the data will 
be converted to UTC since these timezones are not considered equal. 
Either use the same version of timezone library or use tz_convert with 
the updated timezone definition. 

変換を行うことができる。

A:をUTC

にナイーブ/時間認識日時をローカライズする tz_localize方法を用いて
data = pd.date_range("7:00", "9:00", freq="30min").tz_localize('UTC') 

B:tz_convertを使用して、 tz認識データを別のタイムゾーンに変換するpandasオブジェクトを変換する方法。その結果

df = pd.DataFrame(index=[1,1,2,2,2], data=data.tz_convert('US/Eastern')) 
df.groupby(level=0).first() 

      0 
1 2016-09-09 07:00:00-04:00 
2 2016-09-09 08:00:00-04:00 

#0 datetime64[ns, US/Eastern] 
#dtype: object 
関連する問題