2013-06-28 68 views
2

我試圖讀取美國/東部時間文件中的時間作爲索引。鑑於11/02/2008是夏令時開關日,因此有兩小時1秒(該順序表示哪一小時是白天對標準)。當試圖本地化時,代碼會失敗,因爲它們是不明確的。現在,pytz模塊有一種方法可以表明日期是否是dst,這在這裏很有用,但是不清楚pandas是否暴露了這個日期。一種解決方案是爲read_csv創建date_parser函數,但是有什麼方法可以使用其他熊貓函數來實現本地化的DatetimeIndex?謝謝。在已經本地時區上創建熊貓時區感知datetimeindex

from pandas import read_csv, DatetimeIndex 
from StringIO import StringIO 

test = 'Time,Number\n\ 
11/02/2008 00:00, 1\n\ 
11/02/2008 01:00, 2\n\ 
11/02/2008 01:00, 3\n\ 
11/02/2008 02:00, 4\n\ 
11/02/2008 03:00, 5\n\ 
11/02/2008 04:00, 6\n' 

df = read_csv(StringIO(test), parse_dates=[0]) #read in the csv 
di = DatetimeIndex(df['Time']) # create a datetime index 
di.tz_localize('US/Eastern') # try to localize to current timezone 
File "/lib/python2.7/site-packages/pandas/tseries/index.py", line 1463, in tz_localize 
new_dates = tslib.tz_localize_to_utc(self.asi8, tz) 
File "tslib.pyx", line 1561, in pandas.tslib.tz_localize_to_utc (pandas/tslib.c:24350) 
AmbiguousTimeError: 2008-11-02 01:00:00 

所需的輸出是:

<class 'pandas.tseries.index.DatetimeIndex'> 
[2008-11-02 00:00:00, ..., 2008-11-02 04:00:00] 
Length: 6, Freq: H, Timezone: US/Eastern 
dr.values 
array(['2008-11-02T00:00:00.000000000-0400', 
    '2008-11-02T01:00:00.000000000-0400', 
    '2008-11-02T01:00:00.000000000-0500', 
    '2008-11-02T02:00:00.000000000-0500', 
    '2008-11-02T03:00:00.000000000-0500', 
    '2008-11-02T04:00:00.000000000-0500'], dtype='datetime64[ns]') 

回答

4

嘗試此。索引最初不在任何時區,所以需要說,嘿你是'UTC',那麼你可以正確定位。

In [24]: x = pd.DatetimeIndex(df['Time']).tz_localize('UTC').tz_convert('US/Eastern') 

In [25]: x 
Out[25]: 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2008-11-01 20:00:00, ..., 2008-11-02 00:00:00] 
Length: 6, Freq: None, Timezone: US/Eastern 

In [26]: x.values 
Out[26]: 
array(['2008-11-01T20:00:00.000000000-0400', 
     '2008-11-01T21:00:00.000000000-0400', 
     '2008-11-01T21:00:00.000000000-0400', 
     '2008-11-01T22:00:00.000000000-0400', 
     '2008-11-01T23:00:00.000000000-0400', 
     '2008-11-02T00:00:00.000000000-0400'], dtype='datetime64[ns]') 
相關問題