Python Pandas - Interpolation of Missing Values



Interpolation is a powerful technique in Pandas that used for handling the missing values in a dataset. This technique estimates the missing values based on other data points of the dataset. Pandas provides the interpolate() method for both DataFrame and Series objects to fill in missing values using various interpolation methods.

In this tutorial, we will learn about the interpolate() methods in Pandas for filling the missing values in a time series data, numeric data, and more using the different interpolation methods.

Basic Interpolation

The Pandas interpolate() method of the both DataFrame and Series objects is used to fills the missing values using different Interpolation strategies. By default, Pandas automatically uses linear interpolation as the default method.

Example

Here is a basic example of calling the interpolate() method for filling the missing values.

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Using the  interpolate() method
result = df.interpolate()
print("\nResultant DataFrame after applying the interpolation:")
print(result)

Following is the output of the above code −

Original DataFrame:
AB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
AB
01.1000.250000
12.3001.733333
23.5003.216667
34.1754.700000
44.85010.000000
55.52514.700000
66.2001.300000
77.9009.200000

Different Interpolating Methods

Pandas supports several interpolation methods, including linear, polynomial, pchip, akima, spline, and more. These methods provide flexibility for filling the missing values depending on the nature of your data.

Example

The following example demonstrates using the interpolate() method with the barycentric interpolation technique.

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Applying the interpolate() with Barycentric method
result = df.interpolate(method='barycentric')

print("\nResultant DataFrame after applying the interpolation:")
print(result)

Following is the output of the above code −

Original DataFrame:
iAB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
AB
01.1000000.250000
12.59642957.242857
23.50000024.940476
34.0614294.700000
44.53142910.000000
55.16071414.700000
66.2000001.300000
77.9000009.200000

Handling Limits in Interpolation

By default, Pandas interpolation fills all the missing values, but you can limit how many consecutive NaN values are filled using the limit parameter of the interpolate() method.

Example

The following example demonstrates filling the missing values of a Pandas DataFrame by limiting the consecutive fills using the limit parameter of the interpolate() method.

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Applying the interpolate() with limit
result = df.interpolate(method='spline', order=2, limit=1)

print("\nResultant DataFrame after applying the interpolation:")
print(result)

Following is the output of the above code −

Original DataFrame:
iAB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
iAB
01.1000000.250000
12.231383-1.202052
23.500000NaN
34.1115294.700000
4NaN10.000000
5NaN14.700000
66.2000001.300000
77.9000009.200000

Interpolating Time Series Data

Interpolation can be applied to the Pandas time series data as well. It is useful when filling gaps in missing data points over time.

Example

Example statement −

import numpy as np
import pandas as pd

indx = pd.date_range("2024-01-01", periods=10, freq="D")
data = np.random.default_rng(2).integers(0, 10, 10).astype(np.float64)
s = pd.Series(data, index=indx)
s.iloc[[1, 2, 5, 6, 9]] = np.nan

print("Original Series:")
print(s)

result = s.interpolate(method="time")

print("\nResultant Time Series after applying the interpolation:")
print(result)

Following is the output of the above code −

Original Series:
DateValue
2024-01-018.0
2024-01-02NaN
2024-01-03NaN
2024-01-042.0
2024-01-054.0
2024-01-06NaN
2024-01-07NaN
2024-01-080.0
2024-01-093.0
2024-01-10NaN
Resultant Time Series after applying the interpolation:
DateValue
2024-01-018.000000
2024-01-026.000000
2024-01-034.000000
2024-01-042.000000
2024-01-054.000000
2024-01-062.666667
2024-01-071.333333
2024-01-080.000000
2024-01-093.000000
2024-01-103.000000
Advertisements