Path: blob/main/crates/polars-python/src/functions/whenthen.rs
7889 views
use polars::lazy::dsl;1use pyo3::prelude::*;23use crate::PyExpr;45#[pyfunction]6pub fn when(condition: PyExpr) -> PyWhen {7PyWhen {8inner: dsl::when(condition.inner),9}10}1112#[pyclass(frozen)]13#[derive(Clone)]14pub struct PyWhen {15inner: dsl::When,16}1718#[pyclass(frozen)]19#[derive(Clone)]20pub struct PyThen {21inner: dsl::Then,22}2324#[pyclass(frozen)]25#[derive(Clone)]26pub struct PyChainedWhen {27inner: dsl::ChainedWhen,28}2930#[pyclass(frozen)]31#[derive(Clone)]32pub struct PyChainedThen {33inner: dsl::ChainedThen,34}3536#[pymethods]37impl PyWhen {38fn then(&self, statement: PyExpr) -> PyThen {39PyThen {40inner: self.inner.clone().then(statement.inner),41}42}43}4445#[pymethods]46impl PyThen {47fn when(&self, condition: PyExpr) -> PyChainedWhen {48PyChainedWhen {49inner: self.inner.clone().when(condition.inner),50}51}5253fn otherwise(&self, statement: PyExpr) -> PyExpr {54self.inner.clone().otherwise(statement.inner).into()55}56}5758#[pymethods]59impl PyChainedWhen {60fn then(&self, statement: PyExpr) -> PyChainedThen {61PyChainedThen {62inner: self.inner.clone().then(statement.inner),63}64}65}6667#[pymethods]68impl PyChainedThen {69fn when(&self, condition: PyExpr) -> PyChainedWhen {70PyChainedWhen {71inner: self.inner.clone().when(condition.inner),72}73}7475fn otherwise(&self, statement: PyExpr) -> PyExpr {76self.inner.clone().otherwise(statement.inner).into()77}78}798081