Linear Interpolation - Conservative Limit

Gap-fill time series using linear interpolation with conservative gap size limit.

Demonstrates linear interpolation with limit=1, filling only the smallest gaps (isolated single missing values). Useful when you want to preserve larger data gaps for other gap-filling methods (e.g., Machine Learning).

Best for: Pre-processing step to remove only isolated missing values.

Linear interpolation with conservative limit

Demonstrates linear interpolation with limit=1, filling only the smallest gaps (isolated single missing values). Useful when you want to preserve larger data gaps for other gap-filling methods (e.g., Machine Learning).

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

import diive as dv

Load and prepare data

df = dv.load_exampledata_parquet()
df = df.loc[df.index.year == 2022].copy()

series = df['NEE_CUT_REF_orig'].copy()

print(f"Data loaded: {len(series)} records from {series.index.min().date()} to {series.index.max().date()}")
print(f"Missing values before: {series.isnull().sum()}")
  > Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.050
seconds).
Data loaded: 17520 records from 2022-01-01 to 2022-12-31
Missing values before: 11412

Conservative gap-filling (limit=1)

Fill only isolated missing values (gaps of 1 record).

series_gapfilled = dv.gapfilling.linear_interpolation(series=series, limit=1, verbose=True)

print(f"Missing values after: {series_gapfilled.isnull().sum()}")
====================Linear Interpolation Gap-Filling Summary====================

-----------------------------------Parameters-----------------------------------
  Method                              Linear Interpolation
  Gap size limit                         1  (max consecutive records)
  Time series length                     17,520  records

-----------------------------------Input Data-----------------------------------
  Total records                          17,520
  Missing values                         11,412  ( 65.14%)

-----------------------------Gap Analysis (limit=1)-----------------------------
  Total gaps detected                     1,601  (separate regions)
  Gaps <= 1 record(s)                       567  (eligible for filling)
  Gaps > 1 record(s)                      1,034  (exceed limit)
  Values to interpolate                     567  (in fillable gaps)
  Values skipped                         10,845  (in too-large gaps)

----------------------------------Output Data-----------------------------------
  Missing values after                   10,845  ( 61.90%)
  Successfully filled                       567  (  4.97% of 11,412)

-----------------------------Gap Size Distribution------------------------------
  Smallest gap                                1  record(s)
  Median gap size                             3  record(s)
  Largest gap                               119  record(s)
  Mean gap size                             7.1  record(s)
================================================================================

Missing values after: 10845

Visualization: before and after

fig = plt.figure(facecolor='white', figsize=(16, 9))
gs = gridspec.GridSpec(1, 2)
gs.update(wspace=0.3, hspace=0.3, left=0.03, right=0.97, top=0.97, bottom=0.03)
ax_input = fig.add_subplot(gs[0, 0])
ax_output = fig.add_subplot(gs[0, 1])

dv.plotting.HeatmapDateTime(series=series).plot(ax=ax_input)
dv.plotting.HeatmapDateTime(series=series_gapfilled).plot(ax=ax_output)

ax_input.set_title("Observed Data (with gaps)", color='black', fontsize=12, fontweight='bold')
ax_output.set_title("Gap-Filled (limit=1, conservative)", color='black', fontsize=12, fontweight='bold')
ax_input.tick_params(left=True, right=False, top=False, bottom=True,
                     labelleft=False, labelright=False, labeltop=False, labelbottom=True)
ax_output.tick_params(left=True, right=False, top=False, bottom=True,
                      labelleft=False, labelright=False, labeltop=False, labelbottom=True)

fig.show()

print("[OK] Linear interpolation (conservative) complete.")
Observed Data (with gaps), Gap-Filled (limit=1, conservative)
[OK] Linear interpolation (conservative) complete.

Total running time of the script: (0 minutes 0.340 seconds)

Gallery generated by Sphinx-Gallery