2025-06-05 08:48:33 +02:00

35 lines
1.0 KiB
Python

import numpy as np
def fwdshift(day, x):
"""
Python implementation of the MATLAB fwdshift function.
Shifts data forward by the specified number of days.
Parameters:
day (int): Number of days to shift forward
x (ndarray): Input array to be shifted
Returns:
ndarray: Shifted array with NaNs at the end
"""
assert day >= 0
if isinstance(x, list):
x = np.array(x)
# Get the shape information
shape = x.shape
if len(shape) == 1:
# 1D array case
y = np.concatenate([x[day:], np.full(day, np.nan)]) if day > 0 else x.copy()
elif len(shape) == 2:
# 2D array case (most common for financial data)
y = np.vstack([x[day:, :], np.full((day, shape[1]), np.nan)]) if day > 0 else x.copy()
elif len(shape) == 3:
# 3D array case
y = np.concatenate([x[day:, :, :], np.full((day, shape[1], shape[2]), np.nan)]) if day > 0 else x.copy()
else:
raise ValueError("Input array has too many dimensions")
return y