Fill in time data in pandas
<p>I have data that is every 15 seconds. But, there are some values that are missing.  These are not tagged with NaN, but simply are not present.  How can I fill in those values?<br>
I have tried to resample, but that also shifts my original data.  So, why doesn't this work:</p>

<pre><code>a=pd.Series([1.,3.,4.,3.,5.],['2016-05-25 00:00:35','2016-05-25 00:00:50','2016-05-25 00:01:05','2016-05-25 00:01:35','2016-05-25 00:02:05'])                                   
a.index=pd.to_datetime(a.index)
a.resample('15S').mean()

In [368]: a
Out[368]: 
2016-05-25 00:00:35    1.0
2016-05-25 00:00:50    3.0
2016-05-25 00:01:05    4.0
2016-05-25 00:01:35    3.0
2016-05-25 00:02:05    5.0
dtype: float64
</code></pre>

<p>It shows me this:</p>

<pre><code>2016-05-25 00:00:30    1.0
2016-05-25 00:00:45    3.0
2016-05-25 00:01:00    4.0
2016-05-25 00:01:15    NaN
2016-05-25 00:01:30    3.0
2016-05-25 00:01:45    NaN
2016-05-25 00:02:00    5.0
Freq: 15S, dtype: float64
</code></pre>

<p>So, I no longer have a value at 00:35 or 00:50.<br>
For my original larger data set, I also end up seeing many NaN value in large groups at the end of the resampled data.<br>
What I would like to do resample my 15s data, to 15s, so whenever there is no data present for a particular time it should use the mean of the values around it to fill it in.  Is there a way to do that?<br>
Also, why does the time basis change when I resample?  My original data starts at 00:00:35 and after resampling it starts at 00:30?  It seems like it got shifted by 5 seconds.<br>
In my example data, all it should have done is created an additional data entry at 00:01:50. </p>

<hr>

<p><strong>Edit</strong></p>

<p>I realized that my data is slightly more complex then I had thought.  The 'base' actually changes part way through it.  If I use the solution below, then it works for part of the data, but then the values stop changing.  For example:</p>

<pre><code>a = pd.Series([1.,3.,4.,3.,5.,6.,7.,8.], ['2016-05-25 00:00:35','2016-05-25 00:00:50','2016-05-25 00:01:05','2016-05-25 00:01:35','2016-05-25 00:02:05','2016-05-25 00:03:00','2016-05-25 00:04:00','2016-05-25 00:06:00'])                                   

In [79]: a
Out[79]: 
2016-05-25 00:00:35    1.0
2016-05-25 00:00:50    3.0
2016-05-25 00:01:05    4.0
2016-05-25 00:01:35    3.0
2016-05-25 00:02:05    5.0
2016-05-25 00:03:00    6.0
2016-05-25 00:04:00    7.0
2016-05-25 00:06:00    8.0
dtype: float64

In [80]: a.index = pd.to_datetime(a.index)

In [81]: a.resample('15S', base=5).interpolate()
Out[81]: 
2016-05-25 00:00:35    1.0
2016-05-25 00:00:50    3.0
2016-05-25 00:01:05    4.0
2016-05-25 00:01:20    3.5
2016-05-25 00:01:35    3.0
2016-05-25 00:01:50    4.0
2016-05-25 00:02:05    5.0
2016-05-25 00:02:20    5.0
2016-05-25 00:02:35    5.0
2016-05-25 00:02:50    5.0
2016-05-25 00:03:05    5.0
2016-05-25 00:03:20    5.0
2016-05-25 00:03:35    5.0
2016-05-25 00:03:50    5.0
2016-05-25 00:04:05    5.0
2016-05-25 00:04:20    5.0
2016-05-25 00:04:35    5.0
2016-05-25 00:04:50    5.0
2016-05-25 00:05:05    5.0
2016-05-25 00:05:20    5.0
2016-05-25 00:05:35    5.0
2016-05-25 00:05:50    5.0
Freq: 15S, dtype: float64
</code></pre>

<p>As you can see it stops interpolating after 2:05, and seems to ignore the data at 3:00,4:00 and 5:00.  </p>

<p>Both @IanS and @piRSquared address the shifting of the base. As for filling <code>NaN</code>s: pandas has methods for forward-filling (<code>.ffill()</code>/<code>.pad()</code>) and backward-filling (<code>.bfill()</code>/<code>.backfill()</code>), but not for taking the mean. A quick way of doing it is by taking the mean manually:</p>

<pre><code>b = a.resample('15S', base=5)
(b.ffill() + b.bfill()) / 2
</code></pre>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>2016-05-25 00:00:35    1.0
2016-05-25 00:00:50    3.0
2016-05-25 00:01:05    4.0
2016-05-25 00:01:20    3.5
2016-05-25 00:01:35    3.0
2016-05-25 00:01:50    4.0
2016-05-25 00:02:05    5.0
Freq: 15S, dtype: float64
</code></pre>

<p>EDIT: I stand corrected: there is a built-in method: <code>.interpolate()</code>.</p>

<pre><code>a.resample('15S', base=5).interpolate()
</code></pre>

python pandas