# Create a Series with missing data

- Canonical: https://33rdsquare.com/power-of-interpolation-in-python-to-fill-missing-values/
- Published: 2024-09-03
- Author: Jordan Brown
- Categories: [Artificial Intelligence & Machine Learning & ChatGPT](https://33rdsquare.com/category/tech/ai/)

---

## Introduction

Missing data is an unavoidable reality in the world of data science. Whether due to data entry errors, measurement issues, or data corruption, holes and gaps in your data can throw a wrench into your analysis. Luckily, Python provides powerful tools to intelligently fill in those missing values through a technique called interpolation.

In this in-depth guide, we‘ll dive into what interpolation is, the different methods available, and how you can leverage interpolation in Python to preprocess incomplete datasets. Through code samples and real-world examples, you‘ll learn how to use libraries like Pandas and SciPy to apply interpolation to series data, dataframes, time series, and more.

By the end, you‘ll be equipped with the knowledge and skills to handle missing data and ensure your analyses and machine learning models have complete, high-quality input. Let‘s get started!

## What is Interpolation?

Interpolation is a mathematical technique for estimating unknown values that lie between known data points. It involves constructing new data points within the range of a discrete set of known data points.

For example, let‘s say you have temperature readings at 9am and 11am, but you‘re missing the 10am measurement. Interpolation allows you to intelligently estimate what the temperature likely was at 10am based on the known values before and after it.

Interpolation is used across a wide range of fields, including data science, signal processing, image processing, computer graphics, and more. Some common applications include:

- Filling in missing, corrupted, or unreliable values in datasets
- Upsampling or downsampling time series data to change frequency
- Smoothing out noisy or irregular data
- Estimating values at spatial locations between measured points
- Constructing continuous functions from discrete data

At its core, interpolation is about using surrounding information to make reasonable estimates and fill in gaps. This is extremely valuable for data preprocessing, as most real-world datasets have at least some missing values.

## Types of Interpolation Methods

There are many different interpolation methods or algorithms that vary in complexity and assumptions made about the data. Here are some of the most common types:

### Nearest Neighbor Interpolation

The simplest method, nearest neighbor (or proximal) interpolation fills missing values with the nearest valid observation. There are two variants:

- Forward fill (ffill) propagates the last known value forward until the next valid value
- Backward fill (bfill) uses the next known value to fill backwards

While easy to understand and compute, nearest neighbor interpolation is often too simplistic and can produce unrealistic jumps or steps in the data.

### Linear Interpolation

Linear interpolation assumes a straight-line relationship between points and estimates the missing value based on where it falls along that line. For two known points (x0,y0) and (x1,y1), the formula for linear interpolation is:

y = y0 + (x – x0) * (y1 – y0) / (x1 – x0)

where x is the unknown point being estimated. Linear interpolation produces smooth, continuous results but may not be appropriate for data with non-linear trends.

### Polynomial Interpolation

Polynomial interpolation fits a polynomial curve of a specified degree through the known points surrounding the missing value. The higher the degree, the more the curve can bend to hit the points.

For a set of n+1 known points, an nth degree polynomial can be constructed that passes exactly through each point. The Lagrange polynomial is a common method for finding the coefficients.

Polynomial interpolation can model more complex, curving relationships but is prone to overfitting and oscillation, especially at higher degrees and near the edges of the data.

### Spline Interpolation

Spline interpolation fits low-degree polynomials between each pair of known points, with the constraint that the curve be continuous and smooth. Cubic splines (3rd degree polynomials) are most common.

Unlike a single polynomial, splines produce a piecewise function with different coefficients for each interval between known points. This allows for a high degree of flexibility while avoiding instability.

### Other Advanced Methods

Beyond these core methods, there are various other interpolation techniques for specialized use cases:

- Kriging uses spatial autocorrelation to make optimal predictions at unobserved locations
- Radial basis functions interpolate multidimensional data using a weighted sum of radial basis functions
- Gaussian process regression is a Bayesian method that models uncertainty
- Sinc/Whittaker-Shannon interpolation is ideal for bandlimited signals

The choice of interpolation method depends on the characteristics of your data, the number of missing values, the desired smoothness/accuracy, and computational constraints. It‘s good practice to experiment with different methods and validate the results.

## Interpolation in Python with Pandas

The Pandas library provides a convenient interface for applying interpolation to Series and DataFrame objects. The interpolate() function can be called on a Series or DataFrame and accepts various arguments to specify the desired interpolation method and options.

### Interpolating Series Data

To fill missing values in a Pandas Series using interpolation:

import pandas as pd
 import numpy as np

s = pd.Series([0, 1, np.nan, 3, 4, 5, np.nan, 7, 8])

s.interpolate(method=‘linear‘)

s.interpolate(method=‘ffill‘)

s.interpolate(method=‘bfill‘)

s.interpolate(method=‘polynomial‘, order=2)

The interpolate() function automatically detects missing values and applies the specified technique to estimate replacements. The method argument can be one of:

- linear: linear interpolation (default)
- ffill/pad: forward fill
- bfill/backfill: backward fill
- nearest: nearest neighbor
- zero: fill with zeros
- slinear: spline interpolation
- quadratic: 2nd order polynomial
- cubic: 3rd order polynomial
- polynomial: arbitrary order polynomial curve
- krogh: cubic Hermite spline
- piecewise_polynomial: piecewise polynomial (specify order)
- from_derivatives: cubic spline using 1st and 2nd derivatives
- pchip: PCHIP 1-d monotonic cubic interpolation
- akima: Akima 1-d interpolation

Additional options can be specified like limit to set a maximum number of consecutive missing values to fill, limit_direction to restrict to forward or backward filling, and limit_area to interpolate only inside or outside missing ranges.

### Interpolating DataFrame Data

DataFrames can be interpolated column-wise by applying the same interpolate() function:

df = pd.DataFrame({‘A‘: [1, 2, np.nan, 4],
 ‘B‘: [5, np.nan, np.nan, 8],
 ‘C‘: [9, np.nan, 11, 12]})

df.interpolate(method=‘linear‘)

df[‘A‘].interpolate(method=‘linear‘)

df.interpolate(method={‘A‘: ‘linear‘, ‘B‘: ‘ffill‘, ‘C‘: ‘polynomial‘})

By default, interpolate() will interpolate values column-by-column. To interpolate along rows instead, set axis=1.

### Interpolating Time Series

Pandas has excellent support for time series data and interpolating at datetime indexes. By specifying method=‘time‘, missing values will be interpolated based on the time elapsed between valid observations.

ts = pd.Series([1, np.nan, 3, np.nan, 5],
 index=pd.date_range(start=‘1/1/2022‘, periods=5, freq=‘H‘))

ts.interpolate(method=‘time‘)

This accounts for the non-uniform spacing between samples. Other options like limit and limit_direction work similarly for time interpolation.

## Interpolation with SciPy

For more advanced interpolation and approximation use cases, the SciPy library provides a suite of functions in its interpolate submodule. These allow for multi-dimensional interpolation, splines, kriging, and more.

Some key functions include:

- interp1d: 1-d interpolation for any input data
- interp2d: 2-d interpolation on regular grids
- griddata: interpolate unstructured n-dimensional data
- RegularGridInterpolator: batch interpolation on a regular grid
- splrep/splev: representation of and evaluation of spline interpolation
- UnivariateSpline/InterpolatedUnivariateSpline: 1-d smoothing splines
- LSQUnivariateSpline: least-squares spline approximation
- BSpline: n-dimensional B-spline approximation
- CubicSpline: cubic spline data interpolator
- Rbf: radial basis function interpolation
- NearestNDInterpolator: nearest-neighbor interpolation in N dimensions
- KroghInterpolator: interpolate function using Krogh‘s algorithm

Here‘s an example of using interp1d for linear 1-d interpolation:

from scipy.interpolate import interp1d
 import numpy as np

x = np.linspace(0, 10, 10)
 y = np.exp(-x/3.0)

f = interp1d(x, y)

xnew = np.linspace(0, 10, 40)
 ynew = f(xnew)

The interpolate submodule provides great flexibility for more complex interpolation tasks beyond the scope of Pandas.

## Advantages of Interpolation

Interpolation offers several key advantages as a technique for handling missing data:

1. Makes use of valid observed data points to estimate missing values, rather than just discarding incomplete cases
2. Produces smooth, continuous estimates that preserve trends in the data
3. Can be applied flexibly to both time series and cross-sectional data
4. Allows for estimation of values at any point, not just those in the original data (useful for upsampling/downsampling)
5. Variety of methods available to suit different data characteristics and assumptions

When faced with gaps in a dataset, interpolation is often a better choice than simply dropping missing values (complete case analysis) or using a single imputation like the mean. By leveraging the surrounding valid data points, interpolation methods can produce more accurate and realistic estimates.

## Limitations and Considerations

As with any technique, it‘s important to understand the limitations and potential issues with interpolation:

1. Interpolation is not appropriate when data are missing not at random (MNAR). If there is systematic bias in which values are missing, interpolation can produce biased estimates.
2. Some interpolation methods can be sensitive to outliers or extreme values, as these can distort the curve fitting.
3. Extrapolation (estimating values outside the range of known points) is much more difficult and error-prone than interpolation.
4. Interpolation may artificially smooth over real fluctuations or discontinuities in the data, giving misleading results.
5. Very large gaps or blocks of missing data can be challenging to interpolate accurately, as less surrounding information is available.

In general, interpolation performs best when missing values are relatively rare and randomly dispersed throughout the dataset. Results should always be checked for reasonableness. When extensive interpolation is necessary, the impact on later analysis should be carefully considered.

## Conclusion

We‘ve covered a lot of ground in this guide to interpolation in Python. To recap:

- Interpolation is a family of techniques for estimating missing data points based on surrounding known values
- Common interpolation methods include linear, polynomial, and spline interpolation
- Interpolation can be applied easily to Pandas Series and DataFrames using the interpolate() function
- SciPy provides additional functionality for advanced interpolation and approximation in 1-d and n-d
- Interpolation is a powerful tool for data preprocessing but has limitations that should be understood

Equipped with this knowledge, you‘re now ready to apply interpolation to make the most of incomplete real-world datasets. Whenever you encounter missing values, think critically about the interpolation method that best suits your data and analysis goals. With Python‘s robust ecosystem, you‘ll be filling gaps and uncovering insights in no time!

---

Source: [Create a Series with missing data](https://33rdsquare.com/power-of-interpolation-in-python-to-fill-missing-values/)
