Note
Go to the end to download the full example code.
Linear Interpolation - Generous Limit¶
Gap-fill time series using linear interpolation with generous gap size limit.
Demonstrates linear interpolation with limit=5, filling gaps up to 5 consecutive missing values. Shows comprehensive summary statistics and side-by-side visualization.
Best for: Gaps smaller than 1 day. For larger gaps, use machine learning methods.
Linear interpolation with generous limit¶
Demonstrates linear interpolation with limit=5, filling gaps up to 5 consecutive missing values. Shows comprehensive summary statistics and side-by-side visualization.
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
Generous gap-filling (limit=5)¶
Fill gaps up to 5 consecutive missing records.
series_gapfilled = dv.gapfilling.linear_interpolation(series=series, limit=5, verbose=True)
print(f"Missing values after: {series_gapfilled.isnull().sum()}")
====================Linear Interpolation Gap-Filling Summary====================
-----------------------------------Parameters-----------------------------------
Method Linear Interpolation
Gap size limit 5 (max consecutive records)
Time series length 17,520 records
-----------------------------------Input Data-----------------------------------
Total records 17,520
Missing values 11,412 ( 65.14%)
-----------------------------Gap Analysis (limit=5)-----------------------------
Total gaps detected 1,601 (separate regions)
Gaps <= 5 record(s) 1,049 (eligible for filling)
Gaps > 5 record(s) 552 (exceed limit)
Values to interpolate 2,026 (in fillable gaps)
Values skipped 9,386 (in too-large gaps)
----------------------------------Output Data-----------------------------------
Missing values after 9,389 ( 53.59%)
Successfully filled 2,023 ( 17.73% 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: 9389
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=5)", 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 (generous) complete.")

[OK] Linear interpolation (generous) complete.
Total running time of the script: (0 minutes 0.343 seconds)