使用多个条件的Pandas逻辑索引

时间:2017-02-10 13:13:04

标签: python pandas dataframe

我有一个由日期时间戳记索引的pandas数据帧$product,我想只保留日期范围内的行:Monday @ 00:00 - Friday @ 21:59。为此,我写了以下这一行:

data_ask_bid

尽管逻辑索引似乎存在问题,因为它抛出错误'具有多个元素的数组的真值是不明确的。使用a.any()或a.all()'。我在代码中哪里出了问题?

2 个答案:

答案 0 :(得分:1)

我认为您可以使用支票值numpy.in1d

mask1 = np.in1d(data_ask_bid.index.weekday, [0,1,2,3])
mask2 = data_ask_bid.index.weekday == 4
mask3 = data_ask_bid.index.hour < 22

mask = mask1 | (mask2 & mask3)

data_ask_bid = data_ask_bid[mask]

样品:

start = pd.to_datetime('2017-02-10 15:00:00')
rng = pd.date_range(start, periods=20, freq='7h')

data_ask_bid = pd.DataFrame({'a': range(20)}, index=rng)  
#print (data_ask_bid)

w = data_ask_bid.index.weekday
mask1 = np.in1d(w, [0,1,2,3])
mask2 = w == 4
mask3 = data_ask_bid.index.hour < 22

mask = mask1 | (mask2 & mask3)
print (mask)
[ True False False False False False False False False  True  True  True
  True  True  True  True  True  True  True  True]

data_ask_bid = data_ask_bid[mask]
print (data_ask_bid)
                      a
2017-02-10 15:00:00   0
2017-02-13 06:00:00   9
2017-02-13 13:00:00  10
2017-02-13 20:00:00  11
2017-02-14 03:00:00  12
2017-02-14 10:00:00  13
2017-02-14 17:00:00  14
2017-02-15 00:00:00  15
2017-02-15 07:00:00  16
2017-02-15 14:00:00  17
2017-02-15 21:00:00  18
2017-02-16 04:00:00  19

<强>计时

start = pd.to_datetime('2017-02-10 15:00:00')
N = 1000000
rng = pd.date_range(start, periods=N, freq='H')

data_ask_bid = pd.DataFrame({'a': range(N)}, index=rng)  
print (data_ask_bid)

def jez(data_ask_bid):
    w = data_ask_bid.index.weekday
    mask1 = np.in1d(w, [0,1,2,3])
    mask2 = w == 4
    mask3 = data_ask_bid.index.hour < 22
    data_ask_bid = data_ask_bid[mask1 | (mask2 & mask3)]
    return (data_ask_bid)

print (jez(data_ask_bid))

print (data_ask_bid[(((data_ask_bid.index.weekday >= 0) & (data_ask_bid.index.weekday <= 3)) | ((data_ask_bid.index.weekday == 4) & (data_ask_bid.index.hour < 22)))])
In [273]: %timeit (jez(data_ask_bid))
10 loops, best of 3: 142 ms per loop

In [274]: %timeit (data_ask_bid[(((data_ask_bid.index.weekday >= 0) & (data_ask_bid.index.weekday <= 3)) | ((data_ask_bid.index.weekday == 4) & (data_ask_bid.index.hour < 22)))])
1 loop, best of 3: 267 ms per loop

答案 1 :(得分:0)

刚刚发现Pandas不能使用0 <= data_ask_bid.index.weekday <= 3类型的子句,所以我需要将它分成两个单独的条款才能使它工作:

data_ask_bid = data_ask_bid[(((data_ask_bid.index.weekday >= 0) & (data_ask_bid.index.weekday <= 3)) | ((data_ask_bid.index.weekday == 4) & (data_ask_bid.index.hour < 22)))]