Gap Detection and Analysis

Identify and characterize missing data periods in time series.

Scans a time series for consecutive NaN values and reports where gaps occur, how long they are, and which gaps are longest. Useful for spotting data loss events and assessing overall data quality.

Best for: Understanding missing data patterns and data availability

Load data

import diive as dv

# Load the full 10-year dataset
data_df = dv.load_exampledata_parquet()

# Get the original NEE series (before any gap-filling)
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.052
seconds).

Find all gaps

# Detect all consecutive missing periods
gf = dv.analysis.GapFinder(
    series=series,
    max_length=None,   # max gap size to include (records); None = no upper limit
    min_length=None,   # min gap size to include (records); None = no lower limit
    sort_results=True,  # sort by GAP_LENGTH descending
)

# print(gf) shows a formatted summary: series info, missing %, gap count,
# longest/median/mean gap, and active filters
print(gf)
======================================================================
  GAP ANALYSIS  --  NEE_CUT_REF_orig
======================================================================
  Series:          175,296 records   (2013-01-01 to 2022-12-31)
  Missing:         117,099 records   (66.8%)
  Gap periods:      15,602

  Longest:           2,633 records   (54 days 20:30:00)
                  2018-03-06 19:15  ->  2018-04-30 15:15
  Median:                3 records
  Mean:                7.5 records

  Filters:      none
======================================================================

Top longest gaps

# GAP_DURATION is auto-computed from the inferred time resolution
print("Top 5 longest gaps:")
print(gf.results[['GAP_START', 'GAP_END', 'GAP_LENGTH', 'GAP_DURATION']].head(5))
Top 5 longest gaps:
            GAP_START             GAP_END  GAP_LENGTH     GAP_DURATION
0 2018-03-06 19:15:00 2018-04-30 15:15:00        2633 54 days 20:30:00
1 2016-09-12 16:15:00 2016-09-22 09:45:00         468  9 days 18:00:00
2 2021-05-18 16:15:00 2021-05-25 13:45:00         332  6 days 22:00:00
3 2018-06-19 22:45:00 2018-06-26 10:45:00         313  6 days 12:30:00
4 2013-11-22 13:15:00 2013-11-28 09:45:00         282  5 days 21:00:00

Filter by gap size

# Only gaps too long for simple interpolation (> 1 day = 48 records at 30min)
long_gaps = dv.analysis.GapFinder(
    series=series,
    max_length=None,   # no upper bound
    min_length=49,     # at least 49 records = just over 1 day at 30min
    sort_results=True,
)
print(long_gaps)

# Only short gaps suitable for linear interpolation (<= 4 records = 2 hours at 30min)
short_gaps = dv.analysis.GapFinder(
    series=series,
    max_length=4,      # at most 4 records = 2 hours at 30min
    min_length=None,   # no lower bound
    sort_results=True,
)
print(short_gaps)
======================================================================
  GAP ANALYSIS  --  NEE_CUT_REF_orig
======================================================================
  Series:          175,296 records   (2013-01-01 to 2022-12-31)
  Missing:         117,099 records   (66.8%)
  Gap periods:          87

  Longest:           2,633 records   (54 days 20:30:00)
                  2018-03-06 19:15  ->  2018-04-30 15:15
  Median:               66 records
  Mean:              117.0 records

  Filters:      min_length=49
======================================================================
======================================================================
  GAP ANALYSIS  --  NEE_CUT_REF_orig
======================================================================
  Series:          175,296 records   (2013-01-01 to 2022-12-31)
  Missing:         117,099 records   (66.8%)
  Gap periods:       9,597

  Longest:               4 records   (0 days 02:00:00)
                  2020-04-23 03:45  ->  2020-04-23 05:15
  Median:                1 records
  Mean:                1.8 records

  Filters:      max_length=4
======================================================================

Visualization

Two-panel figure:

  • top: daily data availability heatmap (day-of-year x year), showing seasonal and annual patterns in data loss

  • bottom: gap length histogram (log scale) with duration reference lines at 1h / 1 day / 1 week, inferred from time resolution

gf.showfig(
    title=f"Gap Analysis — {series.name}",  # figure suptitle
    saveplot=False,  # set True to save figure to disk
    path=None,       # output directory when saveplot=True
)
Gap Analysis — NEE_CUT_REF_orig, Daily data availability — NEE_CUT_REF_orig  (130 fully missing days, 3522 partial days out of 3652), Gap length distribution  |  15602 gaps  |  117099 missing records total  |  longest: 2633 records

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

Gallery generated by Sphinx-Gallery