Giter Club home page Giter Club logo

polars-plugin-example's Introduction

See https://github.com/pola-rs/pyo3-polars for more details.

expressions.rs

use polars::prelude::*;
use pyo3_polars::derive::polars_expr;

#[polars_expr(output_type=Int64)]
fn min_time(inputs: &[Series]) -> PolarsResult<Series> {
    let time = inputs[0].i64()?;
    let minutes = inputs[1].i64()?;

    let initial_value = time.get(0).unwrap() + minutes.get(0).unwrap();

    let out = time
        .into_iter()
        .zip(minutes.into_iter())
        .scan(initial_value, |state, (time, minutes)| {
            let time = time?;
            let minutes = minutes?;
            if *state > time {
                *state = time + minutes
            }
            Some(*state)
        })
        .collect();
    Ok(out)
}

run.py

import polars as pl
from expression_lib import Accumulate

df = pl.DataFrame({
    "Time": [5, 3, 4, 1, 2],
    "Minutes": [2, 1, 2, 1, 3],
})

print(
    df.with_columns(
       cum_min_time = pl.col("Time").accumulate.min_time("Minutes")
    )
)

Output:

% cd example/derive_expression
% make run
shape: (5, 3)
┌──────┬─────────┬──────────────┐
│ Time ┆ Minutes ┆ cum_min_time │
│ ---  ┆ ---     ┆ ---          │
│ i64  ┆ i64     ┆ i64          │
╞══════╪═════════╪══════════════╡
│ 5    ┆ 2       ┆ 7            │
│ 3    ┆ 1       ┆ 4            │
│ 4    ┆ 2       ┆ 4            │
│ 1    ┆ 1       ┆ 2            │
│ 2    ┆ 3       ┆ 2            │
└──────┴─────────┴──────────────┘

polars-plugin-example's People

Contributors

cmdlineluser avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.