Skip to content

Akima interpolation bug #21276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dershow opened this issue May 31, 2018 · 2 comments
Open

Akima interpolation bug #21276

dershow opened this issue May 31, 2018 · 2 comments
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@dershow
Copy link

dershow commented May 31, 2018

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
from scipy import interpolate


df=pd.DataFrame([1,2,3,np.NaN,np.NaN,np.NaN,np.NaN,np.NaN,np.NaN,4,5],index=[pd.Timestamp('20180531 12:00:00'),pd.Timestamp('20180531 12:00:01'),pd.Timestamp('20180531 12:00:02.1'),pd.Timestamp('20180531 12:00:03.3'),pd.Timestamp('20180531 12:00:04'),pd.Timestamp('20180531 12:00:05'),pd.Timestamp('20180531 12:00:06.3'),pd.Timestamp('20180531 12:00:07'),pd.Timestamp('20180531 12:00:08'),pd.Timestamp('20180531 12:00:09'),pd.Timestamp('20180531 12:00:10.1')],columns=['data'])


df['akima']=df['data'].interpolate(method='akima')
df['pchip']=df['data'].interpolate(method='pchip')
df['time_interp']=df['data'].interpolate(method='time')
df['polynomial']=df['data'].interpolate(method='polynomial',order=3)


plt.plot(df['data'],'o')
plt.plot(df['akima'])
plt.plot(df['pchip'])
plt.plot(df['time_interp'])
plt.plot(df['polynomial'])

f_ak=interpolate.Akima1DInterpolator(df['data'].dropna().index.values.astype('d'),df['data'].dropna())

df['akima scipy']=f_ak(df['data'].index.values.astype('d'))

plt.plot(df['akima scipy'])

plt.legend()
plt.show()

Problem description

Interpolation using the akima method diverges from any reasonable expected result. Above I have some simple sample data, similar to my actual case, and other interpolators gives reasonable results, as demonstrated above. But, the akima method diverges away from the actual data.
I've also used the scipy akima interpolator explicitly above, and that works as expected. So, I would guess that there is a problem in how the data is being converted from pandas to scipy before Akima1DInterpolator is being called in pandas.

Expected Output

interplate(method='akima') should give results that are somewhat near to a linear interpolate, but instead they diverge.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 2.7.15.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: 0.28.2
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: 4.1.1
bs4: 4.5.3
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@chris-b1
Copy link
Contributor

chris-b1 commented Jun 1, 2018

Here's your chart
image

Not knowing anything about this interpolation, seems potentially buggy, further investigation and PR are welcome

@dershow
Copy link
Author

dershow commented Jun 4, 2018

Thanks. I probably should have included the chart as well.
I did another test that further shows that the problem is how the datetimeindex is being passed to scipy. By just changing the index like this, I can get the expected results:

`
df['d']=df['data'].index.values.astype('d')

df.set_index('d',inplace=True)

df['akima_d']=df['data'].interpolate(method='akima')
`

@dershow dershow changed the title Akima interpolation diverges Akima interpolation bug Jun 5, 2018
@mroeschke mroeschke added Bug Numeric Operations Arithmetic, Comparison, and Logical operations labels Jan 13, 2019
@jbrockmendel jbrockmendel added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Sep 21, 2020
@mroeschke mroeschke removed the Numeric Operations Arithmetic, Comparison, and Logical operations label Jun 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

4 participants