def plot_seasonality( self, order="default", center=True, model="additive", median=False, title="Seasonality", x_lab="Index", y_lab="Y", time_index=True, x_rotation=45, **kwargs ): """ Plots series seasonality. """ y = self.values["X"] x = self.values["index"].astype("O") if time_index else np.arange(len(y)) order = self.frequency if order is "default" else order avg_seasonality = seasonality(y, order, center, model, median) plt.plot(x, avg_seasonality, **kwargs) plt.title(title) plt.ylabel(y_lab) plt.xlabel(x_lab) plt.xticks(rotation=x_rotation) plt.tight_layout()
def impute_random(self, trailing=True, mode=None, **kwargs): """ Returns tseries object with missing values of the series filled with a random number from the series up to that point. """ data = self.values["X"] order = kwargs.pop("order", self.frequency) if mode is "decompose": seasonal = seasonality(data, order, **kwargs).reshape(len(data)) de_seasonal = data - seasonal self.values["X"] = seasonal + nan_random(de_seasonal, trailing) elif mode is "split": k = 0 r = [] while k < order: r.append(nan_random(data[k::order], trailing)) k += 1 rz = np.array(list(zip_longest(*r, fillvalue=np.nan))).flatten() self.values["X"] = np.array([i for i in rz if not np.isnan(i).any()]) else: self.values["X"] = nan_random(data, trailing) return self
def decompose(self, order="default", center=True, model="additive", median=False): """ Returns array of decomposition results in the order of: original series, trend, seasonality, random. """ data = self.values["X"] order = self.frequency if order is "default" else order trends = trend(data, order, center) avg_seasonality = seasonality(data, order, center, model, median) random = remainder(data, order, center, model, median) decomp_series = np.column_stack((data, trends, avg_seasonality, random)) dtypes = np.dtype( [ ("series", data.dtype), ("trend", trends.dtype), ("seasonality", avg_seasonality.dtype), ("random", random.dtype), ] ) decomposition = np.empty(len(decomp_series), dtype=dtypes) decomposition["series"] = decomp_series[:, 0] decomposition["trend"] = decomp_series[:, 1] decomposition["seasonality"] = decomp_series[:, 2] decomposition["random"] = decomp_series[:, 3] return decomposition
def impute_interpolation(self, mode=None, **kwargs): """ Returns tseries object with missing values of the series filled via linear interpolation. """ data = self.values["X"] order = kwargs.pop("order", self.frequency) if mode is "decompose": seasonal = seasonality(data, order, **kwargs).reshape(len(data)) de_seasonal = data - seasonal self.values["X"] = seasonal + nan_linear_interpolation(de_seasonal) elif mode is "split": k = 0 r = [] while k < order: r.append(nan_linear_interpolation(data[k::order])) k += 1 rz = np.array(list(zip_longest(*r, fillvalue=np.nan))).flatten() self.values["X"] = np.array([i for i in rz if not np.isnan(i).any()]) else: self.values["X"] = nan_linear_interpolation(data) return self
def decompose_seasonality( self, order="default", center=True, model="additive", median=False ): """ Returns array of series seasonality. """ data = self.values["X"] order = self.frequency if order is "default" else order avg_seasonality = seasonality(data, order, center, model, median) return avg_seasonality