# Time series interpolation

**URL:** <https://discourse.julialang.org/t/time-series-interpolation/77147>\
**Category:** General Usage\
**Tags:** dates, time-series, interpolations\
**Created:** [February 27, 2022, 11:30am UTC](https://discourse.julialang.org/t/time-series-interpolation/77147 "2022-02-27T11:30:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cripcate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cripcate/32/34170_2.png) [@cripcate](https://discourse.julialang.org/u/cripcate)\
**Post date:** [February 27, 2022, 11:30am UTC](https://discourse.julialang.org/t/time-series-interpolation/77147/1 "2022-02-27T11:30:50Z")

</div>

Suppose i have two vectors, one containing timestamps and one containing values:

```julia
using Dates
dt = collect(Date(1900,1,1):Day(2):Date(1902,1,1))
y = randn(length(x))

```

I want to upsample these data to daily resolution by interpolation. However, I couldn’t find a way to do this yet. I tried interpolating with `Interpolations.jl`, but when using the dates as index it never finishes. When I use a “helper index” that is just a number range, I get a `MethodError`, because it can’t interpolate the dates I guess.

```julia
using Interpolations
LinearInterpolation(dt, y) # This command never finishes

i = range(1, len(dt))

LinearInterpolation(i, dt) # MethodError: no method matching *(::Float64, ::Date)

```

Ialso tried to put these vectors in a `TimeArray` from `TimeSeries.jl` and tried using `TimeSeriesResampler`:

```julia
using TimeSeries, TimeSeriesResampler
ts = TimeArray(dt, y)
resample(ts[:A], Day(1))

```

This correctly constructs a `TimeSeriesResampler` object. However, in the documentation I only see options like `sum` or `mean` to aggregate data to a lower resolution and no options for interpolation.

Can anyone point me to some ideas on how to interpolate time series data (linearly)?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 27, 2022, 11:53am UTC](https://discourse.julialang.org/t/time-series-interpolation/77147/2 "2022-02-27T11:53:12Z")

</div>

One option is to convert the dates to number of days:

```julia
itp = LinearInterpolation(Dates.days.(dt), y)
dt1 = Date(1900,1,1):Day(1):Date(1902,1,1)
y1 = itp(Dates.days.(dt1))

```

Anoter way of getting number of days as floats is:

```julia
(dt .- dt[1])/Day(1)

```

_NB:_  
_As_ @DNF _would say, do not use collect, unless really necessary._

---

<div class="post-metadata">

**Author:** ![cripcate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cripcate/32/34170_2.png) [@cripcate](https://discourse.julialang.org/u/cripcate)\
**Post date:** [February 27, 2022, 12:21pm UTC](https://discourse.julialang.org/t/time-series-interpolation/77147/3 "2022-02-27T12:21:29Z")

</div>

Great and simple solution. I guess it will also work correctly with irregularly sampled timeseries, yay! Two follow up questions:

1. How to get back to a date from the integer day values? `Dates.Date.(Dates.days.(dt1))` doesn’t work.

2. Is there a similar thing if the timestamps are sub-daily resolution? Like `Dates.seconds.(dt1)`? Tried this but the method doesnt exist…

**EDIT:** Found it. For conversion of dates to days since epoch and vice versa you can use `eds = Dates.date2epochdays(Date(1900, 1, 1))` and `Dates.epochdays2date(eds)`. This also works with datetime (useful for timeframes samaller than days) with `ems = Dates.datetime2epochms(DateTime(1900, 1, 1))` and `Dates.epochms2datetime(ems)`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 27, 2022, 12:44pm UTC](https://discourse.julialang.org/t/time-series-interpolation/77147/4 "2022-02-27T12:44:33Z")

</div>

There is:

```julia
Dates.datetime2epochms.(DateTime.(dt))

```

which takes DateTime and returns the number of milliseconds since the rounding epoch (0000-01-01T00:00:00).

The inverse is: `Dates.epochms2datetime()`

The use of these functions is probably preferable.
