Note
Go to the end to download the full example code.
Gap Statistics and Distribution¶
Characterize the spatial and temporal distribution of missing data in a time series.
Extends basic gap detection (GapFinder) with monthly
and annual breakdowns, explicit long-gap listing, and a four-panel figure.
Use this when you need to understand when gaps cluster (seasonal bias),
how many qualify as long gaps, and what the year-by-year coverage looks like.
Best for: Pre-gap-filling data quality assessment, flux post-processing chain audits
Load data¶
import diive as dv
data_df = dv.load_exampledata_parquet()
# Use the original (unfilled) NEE series — the series that will be gap-filled
series = data_df['NEE_CUT_REF_orig'].copy()
> 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.058
seconds).
Run gap statistics¶
long_gap_records=48 means “flag any gap >= 48 consecutive missing records as long” — on 30-min data that is exactly 1 day. Adjust to match your sampling frequency and definition of a critical gap.
gs = dv.analysis.GapStats(
series=series,
long_gap_records=48, # 48 records = 1 day at 30-min resolution
)
Rich console report¶
.report() prints a colour-coded summary: overview stats, long-gap table, monthly missing-data table, and annual coverage table. Worst month and worst year are highlighted in yellow.
────────────────────── Gap Analysis -- NEE_CUT_REF_orig ──────────────────────
Series 175,296 records (2013-01-01 -> 2022-12-31)
Missing 117,099 records (66.8%)
Gap periods 15,602
Long gaps 94 (>= 48 records)
────────────────────────── Long Gaps (>= 48 records) ──────────────────────────
# Start End Records Duration
1 2018-03-06 19:15 2018-04-30 15:15 2,633 54 days 20:30:00
2 2016-09-12 16:15 2016-09-22 09:45 468 9 days 18:00:00
3 2021-05-18 16:15 2021-05-25 13:45 332 6 days 22:00:00
4 2018-06-19 22:45 2018-06-26 10:45 313 6 days 12:30:00
5 2013-11-22 13:15 2013-11-28 09:45 282 5 days 21:00:00
6 2021-12-29 14:15 2022-01-03 11:15 235 4 days 21:30:00
7 2018-09-13 21:45 2018-09-17 20:15 190 3 days 23:00:00
8 2020-09-25 08:45 2020-09-28 12:45 153 3 days 04:30:00
9 2017-11-14 14:45 2017-11-17 12:15 140 2 days 22:00:00
10 2021-07-24 16:45 2021-07-27 13:15 138 2 days 21:00:00
11 2018-11-23 16:15 2018-11-26 10:45 134 2 days 19:00:00
12 2021-12-15 19:45 2021-12-18 07:45 121 2 days 12:30:00
13 2021-01-09 02:15 2021-01-11 11:15 115 2 days 09:30:00
14 2021-11-08 02:15 2021-11-10 10:15 113 2 days 08:30:00
15 2018-10-08 10:45 2018-10-10 17:15 110 2 days 07:00:00
16 2017-01-08 16:45 2017-01-10 16:45 97 2 days 00:30:00
17 2021-06-08 10:45 2021-06-10 10:15 96 2 days 00:00:00
18 2014-10-21 16:45 2014-10-23 15:15 94 1 days 23:00:00
19 2013-11-06 15:45 2013-11-08 14:15 94 1 days 23:00:00
20 2016-11-15 11:45 2016-11-17 07:45 89 1 days 20:30:00
... 74 more long gaps not shown
──────────────────── Monthly Missing (all years combined) ─────────────────────
Month Missing% Records missing Gap periods
Jan 81.1% 12,068 1394
Feb 73.5% 9,944 1502
Mar 69.2% 10,299 1431
Apr 61.5% 8,857 1250
May 55.1% 8,205 1268
Jun 55.3% 7,963 1195
Jul 56.1% 8,348 1138
Aug 58.8% 8,742 1213
Sep 64.7% 9,320 1102
Oct 68.7% 10,217 1350
Nov 76.9% 11,079 1309
Dec 81.0% 12,057 1450
─────────────────────────────── Annual Coverage ────────────────────────────────
Year Coverage% Valid Missing
2013 31.4% 5,496 12,024
2014 31.8% 5,573 11,947
2015 34.9% 6,110 11,410
2016 34.8% 6,106 11,462
2017 34.6% 6,063 11,457
2018 26.5% 4,643 12,877
2019 36.3% 6,352 11,168
2020 33.8% 5,938 11,630
2021 33.1% 5,808 11,712
2022 34.9% 6,108 11,412
Programmatic access — summary dict¶
s = gs.summary
print(f"Total missing: {s['total_missing_records']:,} records ({s['missing_pct']:.1f}%)")
print(f"Long gaps (>= {s['long_gap_records_threshold']} records): {s['n_long_gaps']}")
print(f"Worst month: {s['worst_month']} ({s['worst_month_missing_pct']:.1f}% missing)")
print(f"Annual coverage: {s['annual_coverage']}")
Total missing: 117,099 records (66.8%)
Long gaps (>= 48 records): 94
Worst month: 1 (81.1% missing)
Annual coverage: {2013: 31.37, 2014: 31.81, 2015: 34.87, 2016: 34.76, 2017: 34.61, 2018: 26.5, 2019: 36.26, 2020: 33.8, 2021: 33.15, 2022: 34.86}
Monthly statistics table¶
Index is MONTH (1-12). Columns: total_records, missing_records, missing_pct, n_gaps. All years in the series are combined.
print(gs.monthly_stats)
total_records missing_records missing_pct n_gaps
MONTH
1 14880 12068 81.10 1394
2 13536 9944 73.46 1502
3 14880 10299 69.21 1431
4 14400 8857 61.51 1250
5 14880 8205 55.14 1268
6 14400 7963 55.30 1195
7 14880 8348 56.10 1138
8 14880 8742 58.75 1213
9 14400 9320 64.72 1102
10 14880 10217 68.66 1350
11 14400 11079 76.94 1309
12 14880 12057 81.03 1450
Annual coverage table¶
Index is YEAR. Columns: total_records, valid_records, missing_records, coverage_pct.
print(gs.annual_coverage)
total_records valid_records missing_records coverage_pct
YEAR
2013 17520 5496 12024 31.37
2014 17520 5573 11947 31.81
2015 17520 6110 11410 34.87
2016 17568 6106 11462 34.76
2017 17520 6063 11457 34.61
2018 17520 4643 12877 26.50
2019 17520 6352 11168 36.26
2020 17568 5938 11630 33.80
2021 17520 5808 11712 33.15
2022 17520 6108 11412 34.86
Long gaps table¶
All gaps >= long_gap_records, sorted longest-first. Columns: GAP_START, GAP_END, GAP_LENGTH, GAP_DURATION, YEAR, MONTH.
print(f"Long gaps (top 10 of {len(gs.long_gaps)}):")
print(gs.long_gaps.head(10))
Long gaps (top 10 of 94):
GAP_START GAP_END ... YEAR MONTH
0 2018-03-06 19:15:00 2018-04-30 15:15:00 ... 2018 3
1 2016-09-12 16:15:00 2016-09-22 09:45:00 ... 2016 9
2 2021-05-18 16:15:00 2021-05-25 13:45:00 ... 2021 5
3 2018-06-19 22:45:00 2018-06-26 10:45:00 ... 2018 6
4 2013-11-22 13:15:00 2013-11-28 09:45:00 ... 2013 11
5 2021-12-29 14:15:00 2022-01-03 11:15:00 ... 2021 12
6 2018-09-13 21:45:00 2018-09-17 20:15:00 ... 2018 9
7 2020-09-25 08:45:00 2020-09-28 12:45:00 ... 2020 9
8 2017-11-14 14:45:00 2017-11-17 12:15:00 ... 2017 11
9 2021-07-24 16:45:00 2021-07-27 13:15:00 ... 2021 7
[10 rows x 6 columns]
Visualization¶
Four-panel figure: - top: daily data availability heatmap (day-of-year x year) - middle: gap spike timeline, one vertical line per gap, tall spikes are long gaps - bottom left: monthly missing-data polar chart (January at the top, clockwise) - bottom right: gap-length histogram on log scale with 1h / 1-day / 1-week reference lines
gs.showfig(
title=f"Gap Statistics -- {series.name}",
saveplot=False,
path=None,
)

Embedding individual panels in a custom figure¶
Every plot method accepts an Axes argument so panels can be mixed with
other diive or matplotlib plots. The polar chart requires a polar Axes
created with projection='polar'.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(16, 8))
gsl = gridspec.GridSpec(2, 2, hspace=0.4, wspace=0.3)
ax_hm = fig.add_subplot(gsl[0, :])
ax_po = fig.add_subplot(gsl[1, 0], projection='polar')
ax_tl = fig.add_subplot(gsl[1, 1])
gs.plot_availability_heatmap(ax=ax_hm)
gs.plot_monthly_polar(ax=ax_po)
gs.plot_gap_spike_timeline(ax=ax_tl)
fig.suptitle(f"Custom layout -- {series.name}", fontsize=12, fontweight='bold')
fig.tight_layout()
plt.show()

/home/docs/checkouts/diive/checkouts/latest/examples/analysis/analysis_gapstats.py:121: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
fig.tight_layout()
Total running time of the script: (0 minutes 2.281 seconds)