get_closest_value
Module for robust time series value interpolation.
This module provides functionality to find the closest value in a time series to a target timestamp, with robust handling of missing values. It implements:
Exact timestamp matching when available
Adaptive window median interpolation when exact match unavailable
Progressive window size expansion until valid values found
Functions
Find closest value to target timestamp with robust handling. |
- 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 valuestarget_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:
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
Window expansion continues until either: - Valid values found - Entire series searched