Created
March 22, 2019 22:22
-
-
Save ericness/0fff3be9fa9ccdb80c585d2dc3e404a5 to your computer and use it in GitHub Desktop.
Interpolate a standardized time series.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import pandas as pd | |
| from scipy.interpolate import CubicSpline | |
| def interpolate_time_series(time_series: pd.Series, n_points: int) -> pd.Series: | |
| """ | |
| Interpolate the pattern in the standardized time series data into `n_points` | |
| number of values at equal intervals between 0 and 1. | |
| :param time_series: | |
| Time series data to model interpolated points on. The time series must | |
| be standardized to have index values between 0 and 1. | |
| :param n_points: | |
| Number of points to interpolate for. | |
| :return: | |
| Series with index `n_points` values between 0 and 1 and the values set | |
| as the interpolated values. | |
| """ | |
| spline = CubicSpline(time_series.index, time_series.values) | |
| interpolation_points = np.linspace(0, 1, n_points).round(5) | |
| return pd.Series( | |
| data=spline(interpolation_points), | |
| index=interpolation_points | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment