3

datetimeindex インデックスを持つデータフレームがあります。インデックス値で単一の行を削除しようとすると、行数は正しく N-1 になりますが、インデックスの時間はシフトします。実際、行の大きなチャンクが最初から切り捨てられ、Nan 値を持つ行のチャンクが最後に追加されます。この「チャンク」のサイズは、時間単位のタイムゾーン オフセット * 1 時間あたりの頻度のようです。再現可能な例を次に示します。

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
4

1 に答える 1