tsod
tsod copied to clipboard
wrong gradient with abs(periods)>1
We should add some documentation to this function but i think it is currently not calculating the gradients correctly if periods > 1. Testing of "monotonically increasing" could also be done in _validation() function? Possible fix below for dt:
def _gradient(
self, data: pd.Series | pd.DataFrame, periods: int = 1
) -> pd.Series | pd.DataFrame:
#OLD dt = data.index.to_series().diff().dt.total_seconds()
dt = data.index.to_series().diff(periods=periods).dt.total_seconds() <<< NEW LINE
if dt.min() < 1e-15:
raise ValueError("Index must be monotonically increasing")
gradient = data.diff(periods=periods) / dt
return gradient