Polars

Misc

Mutate

  • Example: 10-day and 50-day Moving Average (source)

    moving_average_pl <- 
      long_pl$with_columns(
        pl$
          col("Price")$
          rolling_mean(10)$
          over("Stock")$
          alias("Price_MA10"),
        pl$
          col("Price")$
          rolling_mean(50)$
          over("Stock")$
          alias("Price_MA50")
    )
    moving_average_pl
    
    moving_average_pl |> 
        as_tibble() |> 
        tidyr::pivot_longer(
            cols = Price:Price_MA50
        ) %>%
        group_by(Stock) |> 
        plot_time_series(as_date(Date), value, .color_var = name, .facet_ncol = 4, .smooth = FALSE)

Summarize

  • Example: min, max, mean by group

    df <- pl$scan_csv(file_name)$
        group_by("state")$
        agg(
            pl$
              col("measurement")$
              min()$
              alias("min_m"),
            pl$
              col("measurement")$
              max()$
              alias("max_m"),
            pl$
              col("measurement")$
              mean()$
              alias("mean_m")
        )$
        collect()

Pivoting

  • Example: pivot_longer (source)

    long_pl = stock_data_pl$unpivot(
        index         = "Date",
        value_name    = "Price",
        variable_name = "Stock"
    )
    long_pl
    
    long_pl %>%
        as_tibble() |> 
        group_by(Stock)  |> 
        timetk::plot_time_series(as_date(Date), Price, .facet_ncol = 4, .smooth = FALSE)

SQL

  • Example: min, max, mean by group

    lf <- pl$LazyFrame(D) 
    
    pl$
      SQLContext(frame = lf)$
      execute(
        "select min(measurement) as min_m, 
                max(measurement) as max_m, 
                avg(measurement) as mean_m 
        from frame 
        group by state"
      )$
      collect()