2015-10-13 15 views
3

我有一個帶有datetimeindex索引的數據框。當我嘗試通過其索引值刪除單個行時,行數正確地變爲N-1,但索引中的時間移位。實際上,一大塊行從一開始就被切碎,然後將一大塊帶有Nan值的行添加到結尾。這個'塊'的大小似乎是我的時區偏移,以小時*我每小時的頻率。這裏有一個可重複的例子:當我嘗試使用datetimeindex在一個pandas數據框中刪除單個行時,它會移動索引

Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin 
In[2]: import pandas 
In[3]: from pytz import timezone 
In[4]: from pandas import Timestamp 

In[5]: print pandas.__version__ 
0.14.0 

In[6]: dti = pandas.DatetimeIndex(start='2014-11-09 00:00:00', freq='15T',periods=2976, tz=timezone('US/Pacific')) 

In[7]: df = pandas.DataFrame({'data':range(2976)},index=dti) 

In[8]: df.head(5) 
Out[8]: 
          data 
2014-11-09 00:00:00-08:00  0 
2014-11-09 00:15:00-08:00  1 
2014-11-09 00:30:00-08:00  2 
2014-11-09 00:45:00-08:00  3 
2014-11-09 01:00:00-08:00  4 

In[9]: df.drop(Timestamp('2014-11-28 11:30:00-08:00')).head(5) 
Out[9]: 
          data 
2014-11-09 08:00:00-08:00 32 
2014-11-09 08:15:00-08:00 33 
2014-11-09 08:30:00-08:00 34 
2014-11-09 08:45:00-08:00 35 
2014-11-09 09:00:00-08:00 36 

In[10]: df.drop(Timestamp('2014-11-28 11:30:00-08:00')).tail(5) 
Out[10]: 
          data 
2014-12-10 06:45:00-08:00 NaN 
2014-12-10 07:00:00-08:00 NaN 
2014-12-10 07:15:00-08:00 NaN 
2014-12-10 07:30:00-08:00 NaN 
2014-12-10 07:45:00-08:00 NaN 

In[11]: df.index 
Out[11]: 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2014-11-09 00:00:00-08:00, ..., 2014-12-09 23:45:00-08:00] 
Length: 2976, Freq: 15T, Timezone: US/Pacific 

In[12]: df.drop(Timestamp('2014-11-28 11:30:00-08:00')).index 
Out[12]: 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2014-11-09 08:00:00-08:00, ..., 2014-12-10 07:45:00-08:00] 
Length: 2975, Freq: None, Timezone: US/Pacific 
+0

我剛剛注意到這個轉換是由UTC /時區偏移量引起的......但是索引上的時區轉換似乎還沒有被刪除。 – cperlmutter

回答

1

你應該指出你使用的是0.17.0。

In [13]: import psycopg2 

In [14]: df = DataFrame(np.arange(10),index=pd.date_range('20130101 09:00:00',periods=10,tz=psycopg2.tz.FixedOffsetTimezone(offset=-480, name=None),freq='H'),columns=['value']) 

In [15]: df 
Out[15]: 
          value 
2013-01-01 09:00:00-08:00  0 
2013-01-01 10:00:00-08:00  1 
2013-01-01 11:00:00-08:00  2 
2013-01-01 12:00:00-08:00  3 
2013-01-01 13:00:00-08:00  4 
2013-01-01 14:00:00-08:00  5 
2013-01-01 15:00:00-08:00  6 
2013-01-01 16:00:00-08:00  7 
2013-01-01 17:00:00-08:00  8 
2013-01-01 18:00:00-08:00  9 

In [16]: df.index 
Out[16]: 
DatetimeIndex(['2013-01-01 09:00:00-08:00', '2013-01-01 10:00:00-08:00', '2013-01-01 11:00:00-08:00', '2013-01-01 12:00:00-08:00', '2013-01-01 13:00:00-08:00', '2013-01-01 14:00:00-08:00', 
       '2013-01-01 15:00:00-08:00', '2013-01-01 16:00:00-08:00', '2013-01-01 17:00:00-08:00', '2013-01-01 18:00:00-08:00'], 
       dtype='datetime64[ns, psycopg2.tz.FixedOffsetTimezone(offset=-480, name=None)]', freq='H') 

In [17]: df.drop(Timestamp('2013-01-01 16:00:00',tz=psycopg2.tz.FixedOffsetTimezone(offset=-480, name=None))) 
Out[17]: 
          value 
2013-01-01 09:00:00-08:00  0 
2013-01-01 10:00:00-08:00  1 
2013-01-01 11:00:00-08:00  2 
2013-01-01 12:00:00-08:00  3 
2013-01-01 13:00:00-08:00  4 
2013-01-01 14:00:00-08:00  5 
2013-01-01 15:00:00-08:00  6 
2013-01-01 17:00:00-08:00  8 
2013-01-01 18:00:00-08:00  9 

所以你需要指定正是您要刪除的元素有關的時區,否則其不會在索引中,否則你會得到一個錯誤:

In [18]: df.drop(Timestamp('2013-01-01 16:00:00')) 
ValueError: labels [Timestamp('2013-01-01 16:00:00')] not contained in axis 

所以請提供可重複的例子。

此外,您可能需要使用read_sql_table而不是read_sql_query(它能正確讀取時區列)。

或者你可能只是想轉換爲「更有用的」時區(如UTC或類似美國/ ......)

In [21]: df.index.tz_convert('UTC') 
Out[21]: 
DatetimeIndex(['2013-01-01 17:00:00+00:00', '2013-01-01 18:00:00+00:00', '2013-01-01 19:00:00+00:00', '2013-01-01 20:00:00+00:00', '2013-01-01 21:00:00+00:00', '2013-01-01 22:00:00+00:00', 
       '2013-01-01 23:00:00+00:00', '2013-01-02 00:00:00+00:00', '2013-01-02 01:00:00+00:00', '2013-01-02 02:00:00+00:00'], 
       dtype='datetime64[ns, UTC]', freq='H') 

或剛落TZ和定位,其中其在(這是我認爲你想要的)

In [27]: df.index.tz_localize(None) 
Out[27]: 
DatetimeIndex(['2013-01-01 09:00:00', '2013-01-01 10:00:00', '2013-01-01 11:00:00', '2013-01-01 12:00:00', '2013-01-01 13:00:00', '2013-01-01 14:00:00', '2013-01-01 15:00:00', '2013-01-01 16:00:00', 
       '2013-01-01 17:00:00', '2013-01-01 18:00:00'], 
       dtype='datetime64[ns]', freq='H') 
相關問題