Examples
Filling in NaN in a Series via linear interpolation.
import numpy as np
import pandas as pd
s = pd.Series([0, 2, np.nan, 5])
s
s.interpolate()
Filling in NaN in a Series by padding, but filling at most two consecutive NaN at a time.
s = pd.Series([np.nan, "single_one", np.nan,
"fill_two_more", np.nan, np.nan,
3.71, np.nan])
s
s.interpolate(method='pad', limit=2)
Filling in NaN in a Series via polynomial interpolation or splines: Both ‘polynomial’ and ‘spline’ methods
require that you also specify an order (int).
s = pd.Series([0, 4, np.nan, 8])
s.interpolate(method='polynomial', order=2)
Fill the DataFrame forward (that is, going down) along each column using linear interpolation.
Note how the last entry in column ‘p’ is interpolated differently, because there is no entry
after it to use for interpolation. Note how the first entry in column ‘q’ remains NaN, because
there is no entry before it to use for interpolation.
df = pd.DataFrame([(0.0, np.nan, -2.0, 2.0),
(np.nan, 3.0, np.nan, np.nan),
(2.0, 3.0, np.nan, 7.0),
(np.nan, 4.0, -4.0, 16.0)],
columns=list('pqrs'))
df
df.interpolate(method='linear', limit_direction='forward', axis=0)
Using polynomial interpolation.
df['s'].interpolate(method='polynomial', order=2)