MCPcopy Create free account
hub / github.com/ScottfreeLLC/AlphaPy / sequence_frame

Function sequence_frame

alphapy/frame.py:318–364  ·  view source on GitHub ↗

r"""Create sequences of lagging and leading values. Parameters ---------- df : pandas.DataFrame The original dataframe. target : str The target variable for prediction. forecast_period : int The period for forecasting the target of the analysis. leade

(df, target, forecast_period=1, leaders=[], lag_period=1)

Source from the content-addressed store, hash-verified

316#
317
318def sequence_frame(df, target, forecast_period=1, leaders=[], lag_period=1):
319 r"""Create sequences of lagging and leading values.
320
321 Parameters
322 ----------
323 df : pandas.DataFrame
324 The original dataframe.
325 target : str
326 The target variable for prediction.
327 forecast_period : int
328 The period for forecasting the target of the analysis.
329 leaders : list
330 The features that are contemporaneous with the target.
331 lag_period : int
332 The number of lagged rows for prediction.
333
334 Returns
335 -------
336 new_frame : pandas.DataFrame
337 The transformed dataframe with variable sequences.
338
339 """
340
341 # Set Leaders and Laggards
342 le_cols = sorted(leaders)
343 le_len = len(le_cols)
344 df_cols = sorted(list(set(df.columns) - set(le_cols)))
345 df_len = len(df_cols)
346
347 # Add lagged columns
348 new_cols, new_names = list(), list()
349 for i in range(lag_period, 0, -1):
350 new_cols.append(df[df_cols].shift(i))
351 new_names += ['%s[%d]' % (df_cols[j], i) for j in range(df_len)]
352
353 # Preserve leader columns
354 new_cols.append(df[le_cols])
355 new_names += [le_cols[j] for j in range(le_len)]
356
357 # Forecast Target(s)
358 new_cols.append(pd.DataFrame(df[target].shift(1-forecast_period)))
359 new_names.append(target)
360
361 # Collect all columns into new frame
362 new_frame = pd.concat(new_cols, axis=1)
363 new_frame.columns = new_names
364 return new_frame

Callers 1

run_analysisFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected