<p>我会这么做:</p>
<p>首先透视市场表,以符号作为索引,日期作为列:</p>
<pre><code># assuming you are using close-to-close returns
market_pivot = market.pivot_table(index='Symbol', columns='Date', values='Close')
</code></pre>
<p>其次,定义一个函数来获取返回:</p>
<pre><code>def get_return(ser, li_duration):
"""
ser : pandas series, represents a row from purchase_df
li_duration : list of int for holding periods
"""
# do some list comprehension here to convert li_duration to end dates
ldt_dates = [...]
date = ser['Acceptance_Date']
symbol = ser['Symbol']
begin_price = market_pivot.loc[symbol, date]
end_price = market_pivot.loc[symbol, ldt_dates]
# calculate returns and replace the indices
return_ser = end_price / begin_price - 1
return_ser.index = [<desired column labels here>]
# append the returns to purchase information
return ser.append(return_ser)
</code></pre>
<p>现在应用此函数逐行购买:</p>
<pre><code>df_result = purchase_df.apply(get_return, li_duration=[1, 3, 6, 12])
</code></pre>
<blockquote>
<p>Disclaimer: didn't actually run the code, just to show thought process</p>
</blockquote>