My code
from pandas import Series
import numpy as np
s = Series([0, 1, np.nan, 3])
print ("Results before")
print (s)
s.interpolate()
print ("Results after")
print (s)
s.astype(float).interpolate(method='linear', inplace=True)
print ("Results after after")
print (s)
My output
python ./test_get2.py
Results before
0 0.0
1 1.0
2 NaN
3 3.0
dtype: float64
Results after
0 0.0
1 1.0
2 NaN
3 3.0
dtype: float64
Results after after
0 0.0
1 1.0
2 NaN
3 3.0
dtype: float64
I'm not sure what I'm doing wrong here. Perhaps I don't have a dependency installed that I need. Any ideas would be helpful.
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-71-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.20.1
pytest: None
pip: 9.0.1
setuptools: 20.7.0
Cython: None
numpy: 1.12.1
scipy: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: 0.7.3
lxml: None
bs4: 4.4.1
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: None
Comment From: max-sixty
In [6]: s.astype(float).interpolate(method='linear')
Out[6]:
0 0.0
1 1.0
2 2.0
3 3.0
dtype: float64
or
In [7]: s.interpolate(method='linear', inplace=True)
In [8]: s
Out[8]:
0 0.0
1 1.0
2 2.0
3 3.0
dtype: float64
Generally you'll find it easier to do s=s.func
, rather than using inplace
Comment From: TomAugspurger
In [86]: s.astype(float) is s
Out[86]: False
So your inplace interpolate
wasn't operating on your variable called s
. Like @MaximilianR said, you're better off avoiding inplace.