解释一些 python 代码。访问不存在的列?

Explain some python code. Accessing columns, which don't exist?

代码源在这里:https://github.com/quantopian/zipline/blob/master/zipline/examples/pairtrade.py

代码块是这样的:

def ols_transform(data, sid1, sid2):
   """Computes regression coefficient (slope and intercept)
   via Ordinary Least Squares between two SIDs.
   """
   p0 = data.price[sid1]
   p1 = sm.add_constant(data.price[sid2], prepend=True)
   slope, intercept = sm.OLS(p0, p1).fit().params

   return slope, intercept

数据框"data"由此创建:

data = load_from_yahoo(stocks=['PEP', 'KO'], indexes={},
                           start=start, end=end)

并且有这样的输出:

                          PEP     KO
Date                                   
2001-01-02 00:00:00+00:00  15.25   9.20
2001-01-03 00:00:00+00:00  16.19   9.54
2001-01-04 00:00:00+00:00  16.55   9.72
2001-01-05 00:00:00+00:00  16.29   9.67
2001-01-08 00:00:00+00:00  16.09   9.79
2001-01-09 00:00:00+00:00  15.74   9.70
2001-01-10 00:00:00+00:00  15.74   9.61
2001-01-11 00:00:00+00:00  15.80   9.88

我的问题是,这是如何工作的?

   p0 = data.price[sid1]:
   p1 = sm.add_constant(data.price[sid2], prepend=True) 

在最后的代码块中,'price' 未定义为列。我不确定为什么可以调用它?它甚至不是数据框的名称。

是不是和导入的包有关?还是我完全错过了什么?

这是由于函数(@batchtransform)的前一行:https://github.com/quantopian/zipline/blob/master/zipline/examples/pairtrade.py#L28 which does some magic. For a more complete description of how this works, see the Quantopian help docs https://www.quantopian.com/help under "Batch Transforms."

但是,请注意,这是一个旧示例,需要重构以使用较新的 history(),它可以实现相同但更快、更清晰的效果。帮助文档也包含对此的描述。