选择由DatetimeIndex索引的Pandas DataFrame的子集以及TimeStamps列表

seu*_*mas 12 python time-series pandas

我有一只大熊猫 DataFrame

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3425100 entries, 2011-12-01 00:00:00 to 2011-12-31 23:59:59
Data columns:
sig_qual    3425100  non-null values
heave       3425100  non-null values
north       3425099  non-null values
west        3425097  non-null values
dtypes: float64(4)
Run Code Online (Sandbox Code Playgroud)

我选择使用的子集,然后DataFrame将其.ix[start_datetime:end_datetime]传递给peakdetect函数,该函数返回两个单独列表中的局部最大值和最小值的索引和值.我提取了最大值的索引位置,并使用DataFrame.index我获得的Pandas TimeStamps列表.

然后我尝试通过传递TimeStamps列表来提取大型DataFrame的相关子集,.ix[]但它似乎总是返回一个空的DataFrame.我可以循环遍历TimeStamps列表并从中获取相关的行,DataFrame但这是一个漫长的过程,我认为ix[]应该接受根据文档的值列表? (虽然我看到,熊猫0.7的示例使用numpy.ndarraynumpy.datetime64)

更新: 下面选择了一个8秒的DataFrame子集,#lines显示了一些值:

y = raw_disp['heave'].ix[datetime(2011,12,30,0,0,0):datetime(2011,12,30,0,0,8)]
#csv representation of y time-series 
2011-12-30 00:00:00,-310.0
2011-12-30 00:00:01,-238.0
2011-12-30 00:00:01.500000,-114.0
2011-12-30 00:00:02.500000,60.0
2011-12-30 00:00:03,185.0
2011-12-30 00:00:04,259.0
2011-12-30 00:00:04.500000,231.0
2011-12-30 00:00:05.500000,139.0
2011-12-30 00:00:06.500000,55.0
2011-12-30 00:00:07,-49.0
2011-12-30 00:00:08,-144.0

index = y.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-12-30 00:00:00, ..., 2011-12-30 00:00:08]
Length: 11, Freq: None, Timezone: None

#_max returned from the peakdetect function, one local maxima for this 8 seconds period
_max = [[5, 259.0]]

indexes = [x[0] for x in _max]
#[5]

timestamps = [index[z] for z in indexes]
#[<Timestamp: 2011-12-30 00:00:04>]

print raw_disp.ix[timestamps]
#Empty DataFrame
#Columns: array([sig_qual, heave, north, west, extrema], dtype=object)
#Index: <class 'pandas.tseries.index.DatetimeIndex'>
#Length: 0, Freq: None, Timezone: None

for timestamp in timestamps:
    print raw_disp.ix[timestamp]
#sig_qual      0
#heave       259
#north        27
#west        132
#extrema       0
#Name: 2011-12-30 00:00:04
Run Code Online (Sandbox Code Playgroud)

更新2:创建了一个gist,它实际上是有效的,因为当从csv加载数据时,时间戳的索引列存储为看似是字符串的numpy对象数组.与索引类型<class 'pandas.tseries.index.DatetimeIndex'>且每个元素都是类型的我自己的代码不同<class 'pandas.lib.Timestamp'>,我认为传递一个列表pandas.lib.Timestamp将与传递单个时间戳相同,这会被视为错误吗?

如果我DataFrame使用索引创建原始字符串作为字符串列表,查询字符串列表工作正常.但它确实显着增加了DataFrame的字节大小.

更新3: 错误只出现在非常大的DataFrame中,我在不同大小的DataFrame上重新编写代码(下面的评论中的一些细节),它似乎发生在270万条记录以上的DataFrame上.使用字符串而不是TimeStamps可以解决问题,但会增加内存使用量.

修复了 最新的github master(18/09/2012),请参阅Wes在页面底部的评论.

Wou*_*ire 18

df.ix [my_list_of_dates]应该可以正常工作.

In [193]: df
Out[193]:
            A  B  C  D
2012-08-16  2  1  1  7
2012-08-17  6  4  8  6
2012-08-18  8  3  1  1
2012-08-19  7  2  8  9
2012-08-20  6  7  5  8
2012-08-21  1  3  3  3
2012-08-22  8  2  3  8
2012-08-23  7  1  7  4
2012-08-24  2  6  0  6
2012-08-25  4  6  8  1

In [194]: row_pos = [2, 6, 9]

In [195]: df.ix[row_pos]
Out[195]:
            A  B  C  D
2012-08-18  8  3  1  1
2012-08-22  8  2  3  8
2012-08-25  4  6  8  1

In [196]: dates = [df.index[i] for i in row_pos]

In [197]: df.ix[dates]
Out[197]:
            A  B  C  D
2012-08-18  8  3  1  1
2012-08-22  8  2  3  8
2012-08-25  4  6  8  1
Run Code Online (Sandbox Code Playgroud)

  • 已于今日(9月18日)在GitHub上修复 (3认同)