title: How to impute missing data within a date range in a table?
body: <p>I have the following problem with imputing the missing or zero values in a table. It seems like it's more of an algorithm problem. I wanted to know if someone could help me out figure this out in python or R.</p>
  
  <pre><code>Asset  Mileage      Date
  -----------------------------------
  A   41,084        01/26/2017 00:00:00
  A   0             01/24/2017 00:00:00
  A   0             01/23/2017 00:00:00
  A   40,864        01/19/2017 00:00:00
  A   0             01/18/2017 00:00:00
  B   5,000         01/13/2017 00:00:00
  B   0             01/12/2017 00:00:00
  B   0             01/11/2017 00:00:00
  B   0             01/10/2017 00:00:00
  B   0             01/09/2017 00:00:00
  B   2,000         01/07/2017 00:00:00
  </code></pre>
  
  <p>for each asset(A,B,etc..) traverse through the records chronologically(date) replace all the zeros with the average of mileage between the points = </p>
  
  <blockquote>
    <p>(earlier mileage that is not zero - later mileage that is not zero) /
    ( number of records from the earlier mileage to the later mileage) +
    the earlier mileage.</p>
  </blockquote>
  
  <p>for instance for the above table the data will look like this after it's fixed</p>
  
  <pre><code>Asset  Mileage      Date
  -----------------------------------
  A   41,084  01/26/2017 00:00:00
  A   40,974  01/24/2017 00:00:00
  A   40,919  01/23/2017 00:00:00 
  A   40,864  01/19/2017 00:00:00
  A   39,800  01/18/2017 00:00:00
  B   5,000   01/13/2017 00:00:00
  B   4,000   01/12/2017 00:00:00
  B   3,500   01/11/2017 00:00:00
  B   3,000   01/10/2017 00:00:00
  B   2,500   01/09/2017 00:00:00
  B   2,000   01/07/2017 00:00:00
  </code></pre>
  
  <p>in the above case for instance the calculation for one of the records is as below:</p>
  
  <blockquote>
    <p>(41084-40864)/4(# of records from 40,864 to 41,084) = 110 + previous
    value(40,864) = 40919</p>
  </blockquote>