diive.keep_records_where

diive.keep_records_where(data, target, condition_var, lower=None, upper=None, inclusive='both', invert=False, set_to_nan=True, verbose=False)[source]

Select records of one variable based on the value of another variable.

Returns the target variable restricted to the records where a second variable (condition_var) falls within [lower, upper]. A non-destructive conditional subselection (the row-on-a-condition analogue of keep_vars / times.keep_daterange): the input is left untouched.

At least one of lower/upper must be given; an unset bound means that side is open (no limit). With invert=False (default), records where condition_var is NaN are never kept (a missing condition can’t satisfy the range); with invert=True they are kept (a missing condition can’t be in the removed range either).

Parameters:
  • data (DataFrame) – DataFrame containing both target and condition_var.

  • target (str) – Column to select records from.

  • condition_var (str) – Column whose value decides which records are kept.

  • lower (float) – Lower limit for condition_var; None = open below.

  • upper (float) – Upper limit for condition_var; None = open above.

  • inclusive (str) – Which bounds are inclusive, passed to Series.between: 'both' (default), 'neither', 'left' or 'right'.

  • invert (bool) – If True, keep records where condition_var is outside the range instead of inside it (i.e. remove the in-range records).

  • set_to_nan (bool) – If True (default), keep the full index and set non-matching records to NaN (preserves regular time spacing). If False, drop the non-matching records and return only the matching ones.

  • verbose (bool) – Print how many records were kept.

Return type:

Series

Returns:

A copy of data[target] restricted to the matching records.

Raises:

ValueError – If a column is missing or both limits are None.