英文:
Rust polars Series::series_equal method asserts even if incorrect
问题
The series_eq method checks if the two Series are approximately equal within a certain tolerance, allowing for small differences due to floating-point precision. This is why the test passes when using transformed_s.series_equal(&expected_s) despite minor differences in the values.
However, when you use the assert_eq! macro directly, it performs a strict equality check and considers even tiny differences as failures. That's why the test fails in that case, as there are slight differences in the floating-point values.
The series_eq method is useful when comparing floating-point values with some tolerance for small variations, making it more suitable for your scenario.
英文:
My test asserts true, even though I have different values in the Series.
In the below test index 0 (100.94947) is wrong but it passes
pub fn convert_tb_to_tib(s: Series) -> Series {
    let result = s.cast(&DataType::Float64).unwrap() * 0.90949470177293;
    result
}
    fn test_convert_tb_to_tib() {
        let test_s = Series::new("", &[100, 200, 300, 400]);
        let transformed_s = convert_tb_to_tib(test_s);
        let expected_s = Series::new("", &[100.94947, 181.89894, 272.848411, 373.797881]);
        transformed_s.series_equal(&expected_s);
    }
test wrangle::tests::test_convert_tb_to_tib ... ok
I can evidence this by using the assert_eq! macro instead:
    fn test_convert_tb_to_tib() {
        let test_s = Series::new("", &[100, 200, 300, 400]);
        let transformed_s = convert_tb_to_tib(test_s);
        let expected_s = Series::new("", &[100.94947, 181.89894, 272.848411, 373.797881]);
        assert_eq!(transformed_s, expected_s);
    }
thread 'wrangle::tests::test_convert_tb_to_tib' panicked at 'assertion failed: `(left == right)`
  left: `shape: (4,)
Series: '' [f64]
[
        90.94947
        181.89894
        272.848411
        363.797881
]`,
 right: `shape: (4,)
Series: '' [f64]
[
        100.94947
        181.89894
        272.848411
        373.797881
]`', src/wrangle.rs:108:9
Why does this test pass when using the series_eq method?
答案1
得分: 1
series_equal() 不会断言任何内容,它只进行比较并返回一个布尔值。
您需要使用 assert!() 来断言其结果:
assert!(transformed_s.series_equal(&expected_s));
但我建议您只使用 assert_eq!()。
英文:
series_equal() doesn't assert anything, it only compares and returns a boolean.
You need to assert!() its result:
assert!(transformed_s.series_equal(&expected_s));
But I recommend you to just use assert_eq!().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论