# How can I interpolate time-series data?

I fetch financial data like this:

data =
FinancialData["SBUX", "Close", {{2013, 1, 1}, {2013, 8, 1}, "Week"}, "Value"];


The above expression will return timeseries formatted data. How would I convert this to a regular list with even spacing (this is very important to me) to make interpolation on this data set work?

The time series that FinancialData creates has the option TemporalRegularity -> Automatic. With this option, this series does not interpolate values.

data =
FinancialData["SBUX", "Close", {{2013, 1, 1}, {2013, 8, 1}, "Week"}, "Value"];


Time series that aren't regularly sampled can interpolate values with the option TemporalRegularity -> True. To demonstrate, let's use TimeSeries to make a list of dates between two of the data samples. Change the data time series to assume regular time samples with TemporalRegularity -> True.

 {dt1, dt2} = data["Dates"][[9;;10]];
dates = DateRange[dt1, dt2];
ts = TimeSeries[data, TemporalRegularity -> True];


The data series does not interpolate values.

data[dates]


{$27.43,$27.43, $27.43,$27.43, $27.43,$27.43, $27.43,$29.33}

With TemporalRegularity -> True, the ts series linearly interpolates values for dates between samples.

ts[dates]


{$27.43,$27.71, $27.98,$28.25, $28.52,$28.79, $29.06,$29.33}

To get evenly-spaced interpolated values for the entire time series, use the first and last dates of the series for the list of dates. With these new dates, ts[dates] gives regular, evenly-spaced values. Here's a plot of the interpolated values.

ts = TimeSeries[data, TemporalRegularity -> True];
{dt1, dt2} = ts /@ {"FirstDate", "LastDate"};
dates = DateRange[dt1, dt2];
DateListPlot[Transpose[{dates, ts[dates]}]]


• Thank you. BTW very elegant response. Do you know how to take the dollar values and convert it into a pure numerical list? e.g. {27.43, 27,71, ...}. – Jon Du Jan 17 at 14:28
• @JonDu I'm happy the answer was helpful... and yes, remove the dollar units from values with QuantityMagnitude. – creidhne Jan 17 at 15:22
• can I ask what [[9 ;; 10]] means in your code? I understand [[]] is to extract the value but what does [[a;;b]] mean? – Jon Du Jan 18 at 12:43
• @JonDu [[a ;; b]] gives parts a through b of an expression, for example, {2, 4, 6, 8, 10}[[2 ;; 4]] gives {4, 6, 8}. See the documentation for Part. I picked the [[9 ;; 10]] samples because they show a clear example of the interpolation. – creidhne Jan 18 at 13:11
• Cool! I have another issue that when I try to see output of data[dates] it always shows 8 elements even though I set the dates interval to 3 months. So when I do QuantityMagnitude[data[dates]] there are 8 values. Is there a way to take out all the price data from the interval I defined? – Jon Du Jan 19 at 16:30

Ummm, I'm not sure what do you want. Likely, just use Normal:

• I think you misunderstood my question, but Normal is a cool function – Jon Du Jan 18 at 12:46
data = FinancialData["SBUX", "Close", {{2013, 1, 1}, {2013, 8, 1}, "Week"}, "Value"]


data["Properties"]

{"DatePath", "Dates", "FinancialProperty", "FirstDate", "FirstTime",
"FirstValue", "LastDate", "LastTime", "LastValue", "Path",
"PathComponent", "PathComponents", "PathFunction", "PathLength",
"Times", "ValueDimensions", "Values"}


You can use the property "Path":

data["Path"]