skfolio.utils.tools.apply_window_size#

skfolio.utils.tools.apply_window_size(X, window_size)[source]#

Return the last window_size observations from the array X.

Parameters:
Xndarray of shape (n_observations,) or (n_observations, n_assets)

Input array from which to extract the last observations. Can be 1D or 2D.

window_sizeint or None

Number of observations to keep from the end of X. If None, returns X unchanged.

Returns:
X_windowedndarray

The last window_size rows of X. If window_size is None, returns the original array unchanged.

Raises:
ValueError

If window_size is not a positive integer or cannot be converted to int.

ValueError

If window_size exceeds the number of observations in X.

Examples

>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
>>> apply_window_size(X, window_size=3)
array([[ 5,  6],
       [ 7,  8],
       [ 9, 10]])