From ede5540f5158c9c731f0a57e63e26c66aa0ed70f Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Mon, 7 Aug 2023 22:32:40 +0300 Subject: [PATCH] handle nan --- ta_lib/core/src/series.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ta_lib/core/src/series.rs b/ta_lib/core/src/series.rs index 0a15e183..650d3f64 100644 --- a/ta_lib/core/src/series.rs +++ b/ta_lib/core/src/series.rs @@ -127,7 +127,11 @@ impl Series { impl> From for Series { fn from(item: T) -> Self { Self { - data: item.as_ref().iter().map(|&x| Some(x)).collect(), + data: item + .as_ref() + .iter() + .map(|&x| if x.is_nan() { None } else { Some(x) }) + .collect(), } } } @@ -185,6 +189,16 @@ mod tests { assert_eq!(result.len(), expected); } + #[test] + fn test_from() { + let source = vec![f64::NAN, 1.0, 2.0, 3.0]; + let expected = vec![None, Some(1.0), Some(2.0), Some(3.0)]; + + let result = Series::from(&source); + + assert_eq!(result, expected); + } + #[test] fn test_empty() { let len = 4;