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