title: computing bond yield and duration in timeseries
body: <p>I have timeseries data of bond trades that look somewhat like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: right;"></th>
  <th style="text-align: left;">refdate</th>
  <th style="text-align: left;">bondcode</th>
  <th style="text-align: right;">price</th>
  <th style="text-align: right;">couponrate</th>
  <th style="text-align: right;">couponfreq</th>
  <th style="text-align: left;">maturitydate</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: right;">0</td>
  <td style="text-align: left;">2006-04-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">110.00</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2006-04-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">110.80</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">2</td>
  <td style="text-align: left;">2006-06-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.00</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">3</td>
  <td style="text-align: left;">2006-09-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.20</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">4</td>
  <td style="text-align: left;">2006-09-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.30</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">5</td>
  <td style="text-align: left;">2006-10-01 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">110.90</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">6</td>
  <td style="text-align: left;">2006-01-18 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.10</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">7</td>
  <td style="text-align: left;">2006-01-18 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.30</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">8</td>
  <td style="text-align: left;">2006-01-18 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">111.20</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  <tr>
  <td style="text-align: right;">9</td>
  <td style="text-align: left;">2006-01-19 00:00:00</td>
  <td style="text-align: left;">AT0000385356</td>
  <td style="text-align: right;">110.60</td>
  <td style="text-align: right;">5</td>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">2012-07-15 00:00:00</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>for this data frame, I am trying to compute two columns: the yield of the bonds and the duration.</p>
  <p>I attempted the following, <strong>but I cannot reconcile the yields I compute with the yields available in the original data set</strong> (some of the bonds are yield quoted and for these, an additional column to the above is filled with yields).</p>
  <pre><code>b['man_yield'] = b.apply(lambda x: np.irr([-x.price] + [x.couponrate for i in range(1,x.maturitydate.year - x.refdate.year)] + [100 + x.couponrate*x.last_c_flag]), axis=1)
  </code></pre>
  <p>where b is the data frame above and last_c_flag indicates whether the last coupon is paid before the maturity date.</p>
  <p>Is there a better approach than using apply as shown above to compute the yields?</p>