<p dir="auto">Calling the pd.Series interpolate with method='time' returns inconsistent results when the first or the last value is NaN.</p>
<p dir="auto">When the first value is NaN, interpolation is <strong>not</strong> performed on the first value. This is, for me, the expected behaviour since interpolation is not possible.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pd.Series(index=[datetime(2017,1,1), datetime(2017,1,2), datetime(2017,1,7)], data=[float('nan'), float('nan'), 3]).interpolate(method='time')"><pre class="notranslate"><span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>), <span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">7</span>)], <span class="pl-s1">data</span><span class="pl-c1">=</span>[<span class="pl-en">float</span>(<span class="pl-s">'nan'</span>), <span class="pl-en">float</span>(<span class="pl-s">'nan'</span>), <span class="pl-c1">3</span>]).<span class="pl-en">interpolate</span>(<span class="pl-s1">method</span><span class="pl-c1">=</span><span class="pl-s">'time'</span>)</pre></div>
<p dir="auto">2017-01-01    NaN<br>
2017-01-02    NaN<br>
2017-01-07    3.0<br>
dtype: float64</p>
<p dir="auto">When the last value is a NaN, 'interpolation' is performed (like a forward-fill). I was expecting to keep the NaN values</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pd.Series(index=[datetime(2017,1,1), datetime(2017,1,2), datetime(2017,1,7)], data=[1, float('nan'), float('nan')]).interpolate(method='time')"><pre class="notranslate"><span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-s1">index</span><span class="pl-c1">=</span>[<span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">1</span>), <span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">2</span>), <span class="pl-en">datetime</span>(<span class="pl-c1">2017</span>,<span class="pl-c1">1</span>,<span class="pl-c1">7</span>)], <span class="pl-s1">data</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-en">float</span>(<span class="pl-s">'nan'</span>), <span class="pl-en">float</span>(<span class="pl-s">'nan'</span>)]).<span class="pl-en">interpolate</span>(<span class="pl-s1">method</span><span class="pl-c1">=</span><span class="pl-s">'time'</span>)</pre></div>
<p dir="auto">2017-01-01    1.0<br>
2017-01-02    1.0<br>
2017-01-07    1.0<br>
dtype: float64</p>
<details>
<h2 dir="auto">pd.show_versions()<br>
INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.5.2.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.4.0-53-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8<br>
LOCALE: en_US.UTF-8<br>
pandas: 0.19.1<br>
nose: None<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: None<br>
numpy: 1.11.3<br>
scipy: 0.18.1<br>
statsmodels: None<br>
xarray: None<br>
IPython: None<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2016.7<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: None<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">See also the discussion at <a href="http://stackoverflow.com/questions/25255496/dataframe-interpolate-extrapolates-over-trailing-missing-data" rel="nofollow">StackOverflow</a>.</p>
<p dir="auto">Linear interpolation on a series with missing data at the end of the array will overwrite trailing missing values with the last non-missing value. In effect, the function extrapolates rather than strictly interpolating.</p>
<p dir="auto">Example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np

a = pd.Series([np.nan, 1, np.nan, 3, np.nan])
a.interpolate()"><pre class="notranslate"><code class="notranslate">import pandas as pd
import numpy as np

a = pd.Series([np.nan, 1, np.nan, 3, np.nan])
a.interpolate()
</code></pre></div>
<p dir="auto">Yields (note the extrapolated 4):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0   NaN
1     1
2     2
3     3
4     4
5     4
dtype: float64"><pre class="notranslate"><code class="notranslate">0   NaN
1     1
2     2
3     3
4     4
5     4
dtype: float64
</code></pre></div>
<p dir="auto">not</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0   NaN
1     1
2     2
3     3
4     4
5     NaN
dtype: float64"><pre class="notranslate"><code class="notranslate">0   NaN
1     1
2     2
3     3
4     4
5     NaN
dtype: float64
</code></pre></div>
<p dir="auto">I believe the fix is something along the lines of changing lines 1545:1546 in core/common.py from</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid], yvalues[firstIndex:][valid])"><pre class="notranslate"><code class="notranslate">result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid], yvalues[firstIndex:][valid])
</code></pre></div>
<p dir="auto">to</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid], yvalues[firstIndex:][valid], np.nan, np.nan)"><pre class="notranslate"><code class="notranslate">result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid], yvalues[firstIndex:][valid], np.nan, np.nan)
</code></pre></div>