Trim Filter

Symmetric trimming approach: remove values below threshold, then remove equal number of values from high end (trimmed mean).

Create test data with synthetic noise

Generate temperature data and add impulse noise.

import diive as dv

df = dv.load_exampledata_parquet()

s = df['Tair_f'].copy()
s = s.loc[s.index.year == 2018].copy()

# Add synthetic impulse noise
s_noise = dv.variables.add_impulse_noise(
    series=s,
    factor_low=-10,
    factor_high=4,
    contamination=0.04,
    seed=42
)
s_noise.name = f"{s.name}+noise"

print("Test data created:")
print(f"  Records: {len(s_noise)}")
print(f"  Valid: {s_noise.notna().sum()}")
print(f"  Range: {s_noise.min():.2f} to {s_noise.max():.2f}°C")
  > 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.049
seconds).
Test data created:
  Records: 17520
  Valid: 17520
  Range: -235.74 to 113.37°C

Trim the whole series (default)

By default the whole series is trimmed against one distribution. Day/night is opt-in, so no location parameters are needed here.

trim_all = dv.outliers.TrimLow(
    series=s_noise,
    lower_limit=-75,
    showplot=True,
    verbose=1
)
trim_all.calc()

flag_all = trim_all.overall_flag
filtered_all = s_noise.copy()
filtered_all.loc[flag_all == 2] = None

print("\nWhole-series trimming results:")
print(f"  Outliers detected: {(flag_all == 2).sum()}")
print(f"  Valid records: {filtered_all.notna().sum()}")
print(f"  Data retained: {100*filtered_all.notna().sum()/s_noise.notna().sum():.1f}%")
TrimLow filter all data: Tair_f+noise, n_outliers = 620
Whole-series trimming results:
  Outliers detected: 620
  Valid records: 16900
  Data retained: 96.5%

Trim nighttime only

Apply trimming to nighttime data only. Requesting a day/night split needs the site coordinates (to detect daytime/nighttime). Useful when daytime varies widely but nighttime is stable.

trim_night = dv.outliers.TrimLow(
    series=s_noise,
    trim_daytime=False,
    trim_nighttime=True,
    lower_limit=-75,
    lat=46.81533,
    lon=9.85591,
    utc_offset=1,
    showplot=True,
    verbose=1
)
trim_night.calc()

flag_night = trim_night.overall_flag
filtered_night = s_noise.copy()
filtered_night.loc[flag_night == 2] = None

print("\nNighttime trimming results:")
print(f"  Outliers detected: {(flag_night == 2).sum()}")
print(f"  Valid records: {filtered_night.notna().sum()}")
print(f"  Data retained: {100*filtered_night.notna().sum()/s_noise.notna().sum():.1f}%")
TrimLow filter nighttime only: Tair_f+noise, n_outliers = 310
Nighttime trimming results:
  Outliers detected: 310
  Valid records: 17210
  Data retained: 98.2%

Trim daytime only

Apply trimming to daytime data only. Useful when daytime has measurement issues but nighttime is reliable.

trim_day = dv.outliers.TrimLow(
    series=s_noise,
    trim_daytime=True,
    trim_nighttime=False,
    lower_limit=-75,
    lat=46.81533,
    lon=9.85591,
    utc_offset=1,
    showplot=True,
    verbose=1
)
trim_day.calc()

flag_day = trim_day.overall_flag
filtered_day = s_noise.copy()
filtered_day.loc[flag_day == 2] = None

print("\nDaytime trimming results:")
print(f"  Outliers detected: {(flag_day == 2).sum()}")
print(f"  Valid records: {filtered_day.notna().sum()}")
print(f"  Data retained: {100*filtered_day.notna().sum()/s_noise.notna().sum():.1f}%")
TrimLow filter daytime only: Tair_f+noise, n_outliers = 310
Daytime trimming results:
  Outliers detected: 310
  Valid records: 17210
  Data retained: 98.2%

Trim daytime and nighttime together

With both enabled, each period is trimmed independently against its own distribution and the flags are combined. Use this when the plausible range differs between day and night and both periods need screening.

trim_both = dv.outliers.TrimLow(
    series=s_noise,
    trim_daytime=True,
    trim_nighttime=True,
    lower_limit=-75,
    lat=46.81533,
    lon=9.85591,
    utc_offset=1,
    showplot=True,
    verbose=1
)
trim_both.calc()

flag_both = trim_both.overall_flag
filtered_both = s_noise.copy()
filtered_both.loc[flag_both == 2] = None

print("\nDaytime + nighttime trimming results:")
print(f"  Outliers detected: {(flag_both == 2).sum()}")
print(f"  Valid records: {filtered_both.notna().sum()}")
print(f"  Data retained: {100*filtered_both.notna().sum()/s_noise.notna().sum():.1f}%")
TrimLow filter daytime/nighttime: Tair_f+noise, n_outliers = 620
Daytime + nighttime trimming results:
  Outliers detected: 620
  Valid records: 16900
  Data retained: 96.5%

Symmetric tails: it also removes high values

For every value below lower_limit, an equal number of the highest values is rejected too, keeping the trim symmetric (the trimmed-mean rationale). So some rejected records sit above the limit, not below it. Use a one-sided method (e.g. AbsoluteLimits) if you only want to drop the low extremes.

rejected_both = s_noise[flag_both == 2]
print("\nWhat the both-modes trim rejected:")
print(f"  Below lower_limit (-75): {(rejected_both < -75).sum()}")
print(f"  At/above lower_limit (high tail): {(rejected_both >= -75).sum()}")
What the both-modes trim rejected:
  Below lower_limit (-75): 310
  At/above lower_limit (high tail): 310

Comparison

Trimming removes symmetric tails from the distribution. Daytime and nighttime filtering can be combined or applied selectively.

print("\nComparison:")
print(f"Original valid: {s_noise.notna().sum()}")
print(f"Whole-series trim: {filtered_all.notna().sum()} retained")
print(f"Nighttime trim: {filtered_night.notna().sum()} retained")
print(f"Daytime trim: {filtered_day.notna().sum()} retained")
print(f"Day+night trim: {filtered_both.notna().sum()} retained")
Comparison:
Original valid: 17520
Whole-series trim: 16900 retained
Nighttime trim: 17210 retained
Daytime trim: 17210 retained
Day+night trim: 16900 retained

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

Gallery generated by Sphinx-Gallery