From c7be05645e1187ec7c100fb7a889905eeef23019 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Fri, 14 Jul 2023 11:41:41 +0300 Subject: [PATCH] fix shift --- ta_lib/core/src/series.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ta_lib/core/src/series.rs b/ta_lib/core/src/series.rs index 60156745..f12e6758 100644 --- a/ta_lib/core/src/series.rs +++ b/ta_lib/core/src/series.rs @@ -44,7 +44,7 @@ impl Series { pub fn shift(&self, n: usize) -> Self { let data = repeat(None) .take(n) - .chain(self.data.iter().cloned().skip(n)) + .chain(self.data.iter().cloned().take(self.len() - n)) .collect(); Self { data } @@ -178,6 +178,17 @@ mod tests { assert_eq!(result, expected); } + #[test] + fn test_shift() { + let source = vec![1.0, 2.0, 3.0, 4.0, 5.0]; + let n = 2; + let expected = vec![None, None, Some(1.0), Some(2.0), Some(3.0)]; + + let result = Series::from(&source).shift(n); + + assert_eq!(result, expected); + } + #[test] fn test_change() { let source = vec![