Python, Pandas, I have a dataframe containing datetimes and values.
# Create an empty DataFrame with 'timestamp' and 'value' columns
df = pd.DataFrame(columns=['timestamp', 'value'])
df.set_index('timestamp', inplace=True)
I append data to that frame over time.
At some point, I want to find the value at a timestamp. If it’s already in the df, great, easy to find.
However, if the time I’m looking for is between two existing values, how can I find that quickly, and interpolate between those two bracketing values? ChatGPT led me on a merry chase with nothing by invalid comparisons.
Here’s what I tried so far, not working:
# Check if the target timestamp exists in the DataFrame
timestamps = df.index
if target_timestamp in timestamps:
# Exact match found
return df.loc[target_timestamp, 'value']
else:
# Use searchsorted to find the insertion point
pos = timestamps.searchsorted(target_timestamp)
if pos == 0 or pos == len(timestamps) - 1:
raise ValueError("Target timestamp is out of bounds for interpolation")
if target_timestamp > timestamps[pos]:
previous_timestamp = timestamps[pos]
next_timestamp = timestamps[pos + 1]
else:
previous_timestamp = timestamps[pos - 1]
next_timestamp = timestamps[pos]
# Interpolating the value
previous_value = df.loc[previous_timestamp, 'value']
next_value = df.loc[next_timestamp, 'value']
# Linear interpolation formula
interpolated_value = previous_value + (next_value - previous_value) *
(target_timestamp - previous_timestamp) / (next_timestamp - previous_timestamp)
return interpolated_value