get_closest_value.get_closest_value

get_closest_value.get_closest_value(df, target_timestamp)[source]

Find closest value to target timestamp with robust handling.

This function searches a time series for the value closest to a target timestamp. If an exact match is not available or contains NaN, it progressively expands a window around the target time until valid values are found, then returns their median.

Parameters:
  • df (pandas.DataFrame) – DataFrame with: - datetime index - Single column of values to interpolate - May contain NaN values

  • target_timestamp (datetime.datetime) – Target timestamp to find closest value for

Returns:

Either: - Exact value if timestamp match found and not NaN - Median of closest valid values using adaptive window

Return type:

float

Notes

The search process: 1. Try exact timestamp match first 2. If no match or value is NaN:

  • Start with window of 10 closest timestamps

  • Expand window by 10 until valid values found

  • Return median of valid values in window

  1. Window expansion continues until either: - Valid values found - Entire series searched