Python dataframe:列值低于指定行的下一行的向量函数?

Python dataframe:列值低于指定行的下一行的向量函数?,python,pandas,vectorization,Python,Pandas,Vectorization,我有OHLC价格数据在一个带有日期时间指数的熊猫数据框中,我正在测试交易进入信号 其中之一是标记一个买入进入模式,在此模式中,先前的显著低点被打破。我已经在下面编写了正确工作的代码,但是如果可能的话,我想对它进行矢量化,以便为“FooBuy”列返回一个向量函数 我计划最终在一个实时系统中使用这段代码,因此拥有一个能够自动响应不断变化的数据的向量将是一个巨大的好处 有人能提出什么建议吗 class FooBuy(PricePattern): def df_apply(self, df):

我有OHLC价格数据在一个带有日期时间指数的熊猫数据框中,我正在测试交易进入信号

其中之一是标记一个买入进入模式,在此模式中,先前的显著低点被打破。我已经在下面编写了正确工作的代码,但是如果可能的话,我想对它进行矢量化,以便为“FooBuy”列返回一个向量函数

我计划最终在一个实时系统中使用这段代码,因此拥有一个能够自动响应不断变化的数据的向量将是一个巨大的好处

有人能提出什么建议吗

class FooBuy(PricePattern):

  def df_apply(self, df):

    if not ("SigLow" in df.columns):
       df["SigLow"] = SigLow().df_apply(df)  # correct pattern

    df["SigLowBrokenAt"] = None
    df["FooBuy"] = False

    for date, row in df[df.SigLow].iterrows():
        df_sorted_lows = df[date:].sort("Low", ascending=False)
        df_later_and_lower = df_sorted_lows[df_sorted_lows.Low < row["Low"]]
        if len(df_later_and_lower) > 0:
           df["SigLowBrokenAt"].ix[date] = df_later_and_lower.ix[0].name

    for date in df.SigLowBrokenAt.dropna():
        df["FooBuy"].ix[date] = True

你能发布样本数据吗,即使是假的。回答者很容易模拟一些OHLC数据,但是当你发布数据时,你也可以发布你的预期结果。同时发布
默认值
列.Done。我删除了
默认值
,使事情变得更简单,因为这与本例无关。进一步更新-我找到了如何符合我想要的模式。真的很简单-只是复制了一个数据帧,然后对其进行了处理,只返回带有结果的序列。我怀疑在所有的迭代中,对于大型数据集,它可能仍然非常慢,因此如果有人对如何删除递归/将其转换为纯向量有任何建议,那么请大声说:)
class PricePattern(object):

  def __init__(self, shift=0, **kwargs):
    self.shift = shift

  def __repr__(self):
    return "{0}({1})".format(self.__class__.__name__, "" if self.shift == 0 else "shift=\"{0}\"".format(self.shift))

  def df_apply(self, df):
    raise NotImplementedError()


class SigLow(PricePattern):

  def __init__(self, **kwargs):
    super(SigLow, self).__init__(**kwargs)

  def df_apply(self, df):
    return (df.Low.shift(self.shift) < df.Low.shift(self.shift + 1)) & \
        (df.Low.shift(self.shift) < df.Low.shift(self.shift - 1))
class TestBase(unittest.TestCase):
  def _create_df(self, data):
    return 1 + pd.DataFrame(data, columns=['Open', 'High', 'Low', 'Close'],
                            index=pd.date_range('2/25/2013 06:00', periods=len(data),
                                                freq='H')) / 100

  def _assert_true_alone(self, series, index):
    for i in arange(len(series)):
        self.assertTrue(series[i] or (i not in index))

  def _assert_all_false(self, series):
    for i in arange(len(series)):
        self.assertFalse(series[i])


class TestFooBuy(TestBase):

  def test_matches(self):
    data1 = [[48, 49, 34, 48],
             [48, 50, 46, 48],
             [47, 49, 38, 48],  # SL
             [48, 59, 48, 58],
             [57, 57, 50, 54],
             [53, 53, 36, 40],  # FB
             [39, 39, 29, 32]]

    df = self._create_df(data1)
    #df["FooBuy"] = FooBuy().df_apply(df)  # correct pattern
    FooBuy().df_apply(df)
    self._assert_true_alone(df.SigLow, [2])
    self._assert_true_alone(df.FooBuy, [5])

  def test_does_not_match(self):
    data1 = [[48, 49, 34, 48],
             [48, 50, 46, 48],
             [47, 49, 38, 48],  # SL
             [48, 59, 48, 58],
             [57, 57, 42, 48],  # SL
             [48, 50, 43, 44],
             [45, 52, 45, 51]]

    df = self._create_df(data1)
    #df["FooBuy"] = FooBuy().df_apply(df, config_defaults)  # correct pattern
    FooBuy().df_apply(df, config_defaults)
    #print df
    self._assert_true_alone(df.SigLow, [2, 4])
    self._assert_all_false(df.FooBuy)