使用两行

时间:2016-08-19 14:58:18

标签: python pandas

使用以下数据:

import datetime,numpy as np,pandas as pd
nan = np.nan

a = pd.DataFrame(  {'price': {datetime.time(9, 0): 1,   datetime.time(10, 0): 0,   datetime.time(11, 0): 3,   datetime.time(12, 0): 4,   datetime.time(13, 0): 7,   datetime.time(14, 0): 6,   datetime.time(15, 0): 5,   datetime.time(16, 0): 4,   datetime.time(17, 0): 0,   datetime.time(18, 0): 2,   datetime.time(19, 0): 4,   datetime.time(20, 0): 7},  'reversal': {datetime.time(9, 0): 1,   datetime.time(10, 0): nan,   datetime.time(11, 0): nan,   datetime.time(12, 0): nan,   datetime.time(13, 0): nan,
  datetime.time(14, 0): 6.0,   datetime.time(15, 0): nan,   datetime.time(16, 0): nan,   datetime.time(17, 0): nan,   datetime.time(18, 0): nan,   datetime.time(19, 0): nan,   datetime.time(20, 0): nan}})


a['target_hit']=nan;
a['target_miss']=nan;
a['reversal1']=a['reversal']+1;
a['reversal2']=a['reversal']-a['reversal'];
a.sort_index(1,inplace=True);

我按如下方式创建了一个子集:

hit = a.ix[:,:-2].dropna()
hit

其中输出显示两行匹配:

         price  reversal  reversal1 reversal2
 09:00:00   1   1.0         2.0     0.0
 14:00:00   6   6.0         7.0     0.0

当我尝试使用这些行来匹配以下内容时,我收到此错误ValueError: operands could not be broadcast together with shapes (2,) (12,)

takeBoth = False
targetIsHit,targetIsMiss = False,False

if takeBoth:

    targetHit = a[(hit['reversal1'].values==a['price'].values) & (hit['reversal1'].index.values<a['price'].index.values)];
    targetMiss = a[(hit['reversal2'].values==a['price'].values) & (hit['reversal2'].index.values<a['price'].index.values)];

    targetIsHit,targetIsMiss = not targetHit.empty, not targetMiss.empty

else:

    targetHit = a[(hit['reversal1'].values==a['price'].values) & (hit['reversal1'].index.values<a['price'].index.values)];
    targetIsHit = not targetHit.empty
    if not targetIsHit:
         targetMiss = a[(hit['reversal2'].values==a['price'].values) & (hit['reversal2'].index.values<a['price'].index.values)]; 
         targetIsMiss = not targetMiss.empty

if targetIsHit:a.loc[hit.index.values,"target_hit"] = targetHit.index.values;
if targetIsMiss:a.loc[hit.index.values,"target_miss"] = targetMiss.index.values;

如果hit = a.ix[:,:-2].dropna()只生成一行,我不会收到此错误。在阅读了这篇文章后,我看到这可能是由于广播规则造成的。

我应该迭代hit中的行来避免这种情况吗?有关如何解决此问题的任何其他建议吗?

1 个答案:

答案 0 :(得分:1)

是的,它必须是for循环...现在我改变它以从命中或错过中选择。

import datetime,numpy as np,pandas as pd;
nan = np.nan;

a = pd.DataFrame(  {'price': {datetime.time(9, 0): 1,   datetime.time(10, 0): 0,   datetime.time(11, 0): 3,   datetime.time(12, 0): 4,   datetime.time(13, 0): 7,   datetime.time(14, 0): 6,   datetime.time(15, 0): 5,   datetime.time(16, 0): 4,   datetime.time(17, 0): 0,   datetime.time(18, 0): 2,   datetime.time(19, 0): 4,   datetime.time(20, 0): 7},  'reversal': {datetime.time(9, 0): 1,   datetime.time(10, 0): nan,   datetime.time(11, 0): nan,   datetime.time(12, 0): nan,   datetime.time(13, 0): nan,
  datetime.time(14, 0): 6.0,   datetime.time(15, 0): nan,   datetime.time(16, 0): nan,   datetime.time(17, 0): nan,   datetime.time(18, 0): nan,   datetime.time(19, 0): nan,   datetime.time(20, 0): nan}});


a['target_hit']=a['target_miss']=nan;
a['reversal1']=a['reversal']+1;
a['reversal2']=a['reversal']-a['reversal'];
a.sort_index(1,inplace=True);

hits = a.ix[:,:-2].dropna();

for row,hit in hits.iterrows():

        forwardRows = [row]<a['price'].index.values

        targetHit = a.index.values[(hit['reversal1']==a['price'].values) & forwardRows][0];
        targetMiss = a.index.values[(hit['reversal2']==a['price'].values) & forwardRows][0];

        if targetHit>targetMiss:
            a.loc[row,"target_miss"] = targetMiss;
        else:
            a.loc[row,"target_hit"] = targetHit;


print '#'*50
print a
'''
    ##################################################
##################################################
          price  reversal  reversal1  reversal2  target_hit target_miss
09:00:00      1       1.0        2.0        0.0         NaN    10:00:00
10:00:00      0       NaN        NaN        NaN         NaN         NaN
11:00:00      3       NaN        NaN        NaN         NaN         NaN
12:00:00      4       NaN        NaN        NaN         NaN         NaN
13:00:00      7       NaN        NaN        NaN         NaN         NaN
14:00:00      6       6.0        7.0        0.0         NaN    17:00:00
15:00:00      5       NaN        NaN        NaN         NaN         NaN
16:00:00      4       NaN        NaN        NaN         NaN         NaN
17:00:00      0       NaN        NaN        NaN         NaN         NaN
18:00:00      2       NaN        NaN        NaN         NaN         NaN
19:00:00      4       NaN        NaN        NaN         NaN         NaN
20:00:00      7       NaN        NaN        NaN         NaN         NaN
'''