polars_ops/series/ops/
negate.rs

1use polars_core::prelude::*;
2
3pub fn negate(s: &Series) -> PolarsResult<Series> {
4    use DataType::*;
5    let out = match s.dtype() {
6        #[cfg(feature = "dtype-i8")]
7        Int8 => s.i8().unwrap().wrapping_neg().into_series(),
8        #[cfg(feature = "dtype-i16")]
9        Int16 => s.i16().unwrap().wrapping_neg().into_series(),
10        Int32 => s.i32().unwrap().wrapping_neg().into_series(),
11        Int64 => s.i64().unwrap().wrapping_neg().into_series(),
12        Float32 => s.f32().unwrap().wrapping_neg().into_series(),
13        Float64 => s.f64().unwrap().wrapping_neg().into_series(),
14        #[cfg(feature = "dtype-decimal")]
15        Decimal(_, _) => {
16            let ca = s.decimal().unwrap();
17            let precision = ca.precision();
18            let scale = ca.scale();
19
20            let out = ca.as_ref().wrapping_neg();
21            out.into_decimal_unchecked(precision, scale).into_series()
22        },
23        #[cfg(feature = "dtype-duration")]
24        Duration(_) => {
25            let physical = s.to_physical_repr();
26            let ca = physical.i64().unwrap();
27            let out = ca.wrapping_neg().into_series();
28            out.cast(s.dtype())?
29        },
30        dt => polars_bail!(opq = neg, dt),
31    };
32    Ok(out)
33}