#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read min read

Interpolation Techniques

Learn to estimate missing values based on surrounding data

Interpolation Techniques

What is Interpolation?

Interpolation guesses missing values based on values around it.

Think of it like this:

  • Monday: 10°C
  • Tuesday: ? (missing)
  • Wednesday: 20°C

Interpolation guesses Tuesday is about 15°C (middle of 10 and 20).

Linear Interpolation

Draws a straight line between known points.

code.py
import pandas as pd

df = pd.DataFrame({
    'Day': [1, 2, 3, 4, 5],
    'Temp': [10, None, None, 20, 25]
})

print("Before:")
print(df)

df['Temp'] = df['Temp'].interpolate()

print("\nAfter interpolation:")
print(df)

Output:

Before: Day Temp 0 1 10.0 1 2 NaN 2 3 NaN 3 4 20.0 4 5 25.0 After interpolation: Day Temp 0 1 10.00 1 2 13.33 <- estimated 2 3 16.67 <- estimated 3 4 20.00 4 5 25.00

Values go smoothly from 10 to 20.

Interpolation vs Forward Fill

code.py
df = pd.DataFrame({
    'Value': [10, None, None, 40]
})

# Forward fill: copies previous value
print("Forward fill:")
print(df['Value'].ffill())

# Interpolate: estimates based on pattern
print("\nInterpolate:")
print(df['Value'].interpolate())

Output:

Forward fill: 0 10.0 1 10.0 <- same as before 2 10.0 <- same as before 3 40.0 Interpolate: 0 10.0 1 20.0 <- goes up smoothly 2 30.0 <- goes up smoothly 3 40.0

When to Use Each

MethodUse When
Forward FillValue stays same until change
InterpolateValue changes gradually

Examples:

  • Forward fill: Stock price (stays same until trade)
  • Interpolate: Temperature (changes gradually)

Limit Interpolation

Don't fill too many missing values in a row:

code.py
df = pd.DataFrame({
    'Value': [10, None, None, None, None, 60]
})

# Only fill up to 2 missing values
df['Value'] = df['Value'].interpolate(limit=2)
print(df)

Output:

Value 0 10.0 1 20.0 <- filled 2 30.0 <- filled 3 NaN <- not filled (limit reached) 4 NaN <- not filled 5 60.0

Key Points

  • interpolate() estimates missing values smoothly
  • Works best for data that changes gradually
  • Default is linear (straight line)
  • Use limit=n to control how many to fill
  • Better than forward fill for trending data

What's Next?

Learn to convert data types - change text to numbers, numbers to dates, and more.

SkillsetMaster - AI, Web Development & Data Analytics Courses