Polars
Misc
Also see Python, Polars
Packages
Resources
Coerce an existing dataframe into a polars dataframe
$ plDataFrame(D)$ # do stuff
Mutate
Example: 10-day and 50-day Moving Average (source)
<- moving_average_pl $with_columns( long_pl$ plcol("Price")$ rolling_mean(10)$ over("Stock")$ alias("Price_MA10"), $ plcol("Price")$ rolling_mean(50)$ over("Stock")$ alias("Price_MA50") ) moving_average_pl |> moving_average_pl as_tibble() |> ::pivot_longer( tidyrcols = 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
<- pl$scan_csv(file_name)$ df group_by("state")$ agg( $ plcol("measurement")$ min()$ alias("min_m"), $ plcol("measurement")$ max()$ alias("max_m"), $ plcol("measurement")$ mean()$ alias("mean_m") $ )collect()
Pivoting
Example:
pivot_longer
(source)= stock_data_pl$unpivot( long_pl index = "Date", value_name = "Price", variable_name = "Stock" ) long_pl %>% long_pl as_tibble() |> group_by(Stock) |> ::plot_time_series(as_date(Date), Price, .facet_ncol = 4, .smooth = FALSE) timetk
SQL
Example: min, max, mean by group
<- pl$LazyFrame(D) lf $ plSQLContext(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()