Home > OS >  Interpolation between two datetimes
Interpolation between two datetimes

Time:11-03

I have a time series dataset and I'm getting some events. These events are when I get a specific error from my system. Now I wanted to plot a graph from the dataset and place markers from the events on the graph of my time series. So I have to interpolate between two timestamps to get the exact y value. My problem is now when I'm typing in the following code:

import numpy as np
test=np.interp(event, [timestamp_timeseries[k-1],
    timestamp_timeseries[k]], [y_value[k-1], y_value[k])

with types:
timestamp_timeseries: datetime.datetime
y_value: int
event: datetime.datetime (Timestamp when an error is coming from a system)
Thanks for you help.

Example:

test=np.interp(
 datetime.datetime(2022, 10, 11, 12, 24, 5, 922000),
 [datetime.datetime(2022, 10, 11, 12, 6, 40, 480000), 
 datetime.datetime(2022, 10, 11, 12, 52, 51, 481000)], 
 [335872, 336896])

My result is:

TypeError: float() argument must be a string or a number, not 'datetime.datetime'

CodePudding user response:

use a numeric representation of datetime, e.g. Unix time you get from .timestamp(). Ex:

from datetime import datetime
import numpy as np

test=np.interp(
    datetime(2022, 10, 11, 12, 24, 5, 922000).timestamp(),
    [datetime(2022, 10, 11, 12, 6, 40, 480000).timestamp(), datetime(2022, 10, 11, 12, 52, 51, 481000).timestamp()], 
    [335872, 336896])

test
# 336258.3342553605
  • Related