### Initialize DataFrame for examples Source: https://www.rubydoc.info/gems/polars-df/Polars/DataFrame%3Amap_rows Setup code used for the subsequent map_rows examples. ```ruby df = Polars::DataFrame.new({"foo" => [1, 2, 3], "bar" => [-1, 5, 8]}) ``` -------------------------------- ### Initialize Series for chunking examples Source: https://www.rubydoc.info/gems/polars-df/Polars/Series%3Achunk_lengths Setup two Series objects to be used in subsequent concatenation examples. ```ruby s = Polars::Series.new("a", [1, 2, 3]) s2 = Polars::Series.new("b", [4, 5, 6]) ``` -------------------------------- ### Example code block Source: https://www.rubydoc.info/gems/polars-df/Polars/Catalog/Unity/TableInfo A generic code block example found in the documentation. ```ruby 5 6 7 ``` -------------------------------- ### row_index Usage Examples Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions Examples demonstrating how to generate row indices in DataFrames and aggregations. ```ruby df = Polars::DataFrame.new({"x" => ["A", "A", "B", "B", "B"]}) df.with_columns(Polars.row_index, Polars.row_index("another_index")) ``` ```ruby df.group_by("x").agg(Polars.row_index).sort("x") ``` ```ruby df.select(Polars.row_index) ``` -------------------------------- ### select Usage Example Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions Example of using Polars.select to run expressions without a context. ```ruby foo = Polars::Series.new("foo", [1, 2, 3]) bar = Polars::Series.new("bar", [3, 2, 1]) Polars.select(min: Polars.min_horizontal(foo, bar)) ``` -------------------------------- ### Initialize DataFrame for Examples Source: https://www.rubydoc.info/gems/polars-df/Polars/Expr%3Abottom_k_by Create a sample DataFrame to demonstrate the bottom_k_by functionality. ```ruby df = Polars::DataFrame.new( { "a" => [1, 2, 3, 4, 5, 6], "b" => [6, 5, 4, 3, 2, 1], "c" => ["Apple", "Orange", "Apple", "Apple", "Banana", "Banana"], } ) ``` -------------------------------- ### Pad string start Source: https://www.rubydoc.info/gems/polars-df/Polars/StringExpr Pads the start of a string until it reaches the specified length using a fill character. ```ruby df = Polars::DataFrame.new({"a": ["cow", "monkey", "hippopotamus", nil]}) df.with_columns(padded: Polars.col("a").str.pad_start(8, "*")) ``` ```ruby # File 'lib/polars/string_expr.rb', line 677 def pad_start(length, fill_char = " ") length = Utils.parse_into_expression(length) Utils.wrap_expr(_rbexpr.str_pad_start(length, fill_char)) end ``` -------------------------------- ### starts_with(prefix) Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryNameSpace Check if values start with a binary substring. ```APIDOC ## starts_with(prefix) ### Description Check if values start with a binary substring. ### Parameters - **prefix** (String) - Required - Prefix substring. ### Returns - **Series** - A Series of booleans indicating if the condition is met. ``` -------------------------------- ### starts_with(prefix) Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryExpr Checks if binary values start with a specified prefix substring. ```APIDOC ## starts_with(prefix) ### Description Check if values start with a binary substring. ### Parameters - **prefix** (String) - Required - Prefix substring to check against. ### Returns - **Expr** - A boolean expression indicating if the binary value starts with the prefix. ``` -------------------------------- ### starts_with Source: https://www.rubydoc.info/gems/polars-df/Polars/CatExpr Checks if the string representations of values start with a specified prefix. ```APIDOC ## starts_with(prefix) ### Description Checks if string representations of values start with a substring. Requires a literal string value. ### Parameters - **prefix** (String) - Required - The prefix substring to check for. ### Returns - **Expr** - The resulting expression. ``` -------------------------------- ### starts_with(prefix) Source: https://www.rubydoc.info/gems/polars-df/Polars/StringExpr Check if string values start with a substring. ```APIDOC ## starts_with(prefix) ### Description Check if string values start with a substring. ### Parameters - **prefix** (String) - Required - Prefix substring. ### Returns - **Expr** ``` -------------------------------- ### Binning Series with qcut Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Examples demonstrating how to divide a Series into categories using predefined probabilities or uniform bin counts. ```ruby s = Polars::Series.new("foo", [-2, -1, 0, 1, 2]) s.qcut([0.25, 0.75], labels: ["a", "b", "c"]) # => # shape: (5,) # Series: 'foo' [cat] # [ # "a" # "a" # "b" # "b" # "c" # ] ``` ```ruby s.qcut(2, labels: ["low", "high"], left_closed: true) # => # shape: (5,) # Series: 'foo' [cat] # [ # "low" # "low" # "high" # "high" # "high" # ] ``` ```ruby cut = s.qcut([0.25, 0.75], include_breaks: true).alias("cut") s.to_frame.with_columns(cut).unnest("cut") # => # shape: (5, 3) # ┌─────┬─────────────┬────────────┐ # │ foo ┆ break_point ┆ category │ # │ --- ┆ --- ┆ --- │ # │ i64 ┆ f64 ┆ cat │ # ╞═════╪═════════════╪════════════╡ # │ -2 ┆ -1.0 ┆ (-inf, -1] │ # │ -1 ┆ -1.0 ┆ (-inf, -1] │ # │ 0 ┆ 1.0 ┆ (-1, 1] │ # │ 1 ┆ 1.0 ┆ (-1, 1] │ # │ 2 ┆ inf ┆ (1, inf] │ # └─────┴─────────────┴────────────┘ ``` -------------------------------- ### Retrieve lower bound of a Series Source: https://www.rubydoc.info/gems/polars-df/Polars/Series%3Alower_bound Examples showing how to get the lower bound for different data types. ```ruby s = Polars::Series.new("s", [-1, 0, 1], dtype: Polars::Int32) s.lower_bound # => # shape: (1,) # Series: 's' [i32] # [ # -2147483648 # ] ``` ```ruby s = Polars::Series.new("s", [1.0, 2.5, 3.0], dtype: Polars::Float32) s.lower_bound # => # shape: (1,) # Series: 's' [f32] # [ # -inf # ] ``` -------------------------------- ### Generate a time range with Polars Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions%3Atime_range Example of generating a time range with a specific start time and interval, returning a Series. ```ruby Polars.time_range( Time.utc(2000, 1, 1, 14, 0), nil, "3h15m", eager: true ).alias("time") ``` -------------------------------- ### Polars::Catalog#initialize Source: https://www.rubydoc.info/gems/polars-df/Polars/Catalog%3Ainitialize Initializes a new catalog client instance with the specified workspace URL and authentication settings. ```APIDOC ## initialize(workspace_url, bearer_token: "auto", require_https: true) ### Description Initializes a catalog client. Note that this functionality is considered unstable and may change at any point. ### Parameters - **workspace_url** (String) - Required - URL of the workspace, or alternatively the URL of the Unity catalog API endpoint. - **bearer_token** (String) - Optional (default: "auto") - Bearer token to authenticate with. Can be set to "auto" to retrieve from environment or "databricks-sdk" to use the Databricks SDK. - **require_https** (Boolean) - Optional (default: true) - Require the workspace_url to use HTTPS. ### Returns - **Catalog** - A new instance of the Catalog client. ``` -------------------------------- ### initialize(workspace_url, bearer_token: "auto", require_https: true) Source: https://www.rubydoc.info/gems/polars-df/Polars/Catalog Initializes a new catalog client instance. This functionality is considered unstable. ```APIDOC ## initialize(workspace_url, bearer_token: "auto", require_https: true) ### Description Initializes a catalog client. This functionality is considered unstable and may change without notice. ### Parameters - **workspace_url** (String) - Required - URL of the workspace, or alternatively the URL of the Unity catalog API endpoint. - **bearer_token** (String) - Optional (default: "auto") - Bearer token to authenticate with. Can be "auto" to retrieve from environment or "databricks-sdk" to use the Databricks SDK. - **require_https** (Boolean) - Optional (default: true) - Require the workspace_url to use HTTPS. ``` -------------------------------- ### Cumulative Fold Example Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions%3Acum_fold Demonstrates using cum_fold to sum values across columns 'a', 'b', and 'c' starting with an initial value of 1. ```ruby df = Polars::DataFrame.new( { "a" => [1, 2, 3], "b" => [3, 4, 5], "c" => [5, 6, 7] } ) df.with_columns( Polars.cum_fold(Polars.lit(1), Polars.all) { |acc, x| acc + x } ) ``` -------------------------------- ### Check boolean Series with all? Source: https://www.rubydoc.info/gems/polars-df/Polars/Series%3Aall%3F Examples demonstrating the behavior of all? on different boolean Series inputs. ```ruby Polars::Series.new([true, true]).all? # => true ``` ```ruby Polars::Series.new([false, true]).all? # => false ``` ```ruby Polars::Series.new([nil, true]).all? # => true ``` -------------------------------- ### Polars::Expr#slice(offset, length = nil) Source: https://www.rubydoc.info/gems/polars-df/Polars/Expr%3Aslice Get a slice of this expression. Negative indexing is supported for the offset. If length is nil, all rows starting at the offset are selected. ```APIDOC ## Polars::Expr#slice ### Description Get a slice of this expression. Negative indexing is supported for the offset. ### Parameters - **offset** (Integer) - Required - Start index. Negative indexing is supported. - **length** (Integer) - Optional - Length of the slice. If set to nil, all rows starting at the offset will be selected. ### Returns - **Expr** - The sliced expression. ### Example ```ruby df = Polars::DataFrame.new( { "a" => [8, 9, 10, 11], "b" => [nil, 4, 4, 4] } ) df.select(Polars.all.slice(1, 2)) ``` ``` -------------------------------- ### Initialize DataFrame for indexing Source: https://www.rubydoc.info/gems/polars-df/Polars/DataFrame%3A%5B%5D Create a sample DataFrame to demonstrate various indexing operations. ```ruby df = Polars::DataFrame.new( {"a" => [1, 2, 3], "d" => [4, 5, 6], "c" => [1, 3, 2], "b" => [7, 8, 9]} ) ``` -------------------------------- ### Execute SQL to show tables Source: https://www.rubydoc.info/gems/polars-df/Polars/SQLContext%3Atables Demonstrates registering a DataFrame in a SQLContext and querying the table list using SQL. ```ruby frame_data = Polars::DataFrame.new({"hello" => ["world"]}) ctx = Polars::SQLContext.new(hello_world: frame_data) ctx.execute("SHOW TABLES", eager: true) # => # shape: (1, 1) # ┌─────────────┐ # │ name │ # │ --- │ # │ str │ # ╞═════════════╡ # │ hello_world │ # └─────────────┘ ``` -------------------------------- ### Implementation of get method Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryExpr%3Aget The internal implementation of the get method in the Polars library. ```ruby def get(index, null_on_oob: false) index_rbexpr = Utils.parse_into_expression(index) Utils.wrap_expr(_rbexpr.bin_get(index_rbexpr, null_on_oob)) end ``` -------------------------------- ### Series#initialize Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Creates a new Series instance. ```APIDOC ## Series#initialize(name = nil, values = nil, dtype: nil, strict: true, nan_to_null: false) ### Description Create a new Series. ### Parameters - **name** (String) - Optional - Name of the series. - **values** (Array) - Optional - Initial values. - **dtype** (Symbol) - Optional - Data type. - **strict** (Boolean) - Optional - Whether to enforce strict typing. - **nan_to_null** (Boolean) - Optional - Whether to convert NaN to null. ``` -------------------------------- ### Implementation of ArrayExpr#get Source: https://www.rubydoc.info/gems/polars-df/Polars/ArrayExpr%3Aget The internal implementation of the get method in the Polars library. ```ruby def get(index, null_on_oob: false) index = Utils.parse_into_expression(index) Utils.wrap_expr(_rbexpr.arr_get(index, null_on_oob)) end ``` -------------------------------- ### Initialize DataFrame for Time Selection Source: https://www.rubydoc.info/gems/polars-df/Polars/Selectors.time Sets up a DataFrame with datetime, date, and time columns to demonstrate selector functionality. ```ruby df = Polars::DataFrame.new( { "dtm" => [DateTime.new(2001, 5, 7, 10, 25), DateTime.new(2031, 12, 31, 0, 30)], "dt" => [Date.new(1999, 12, 31), Date.new(2024, 8, 9)], "tm" => [Time.utc(2001, 1, 1, 0, 0, 0), Time.utc(2001, 1, 1, 23, 59, 59)] }, schema_overrides: {"tm" => Polars::Time} ) ``` -------------------------------- ### pad_start(length, fill_char = " ") Source: https://www.rubydoc.info/gems/polars-df/Polars/StringExpr Pad the start of the string until it reaches the given length. ```APIDOC ## pad_start(length, fill_char = " ") ### Description Pad the start of the string until it reaches the given length. Strings with length equal to or greater than this value are returned as-is. ### Parameters - **length** (Integer) - Required - Pad the string until it reaches this length. - **fill_char** (String) - Optional (default: " ") - The character to pad the string with. ### Returns - **Expr** - The resulting expression. ``` -------------------------------- ### Implementation of the get method Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryNameSpace%3Aget The source code definition for the get method within the BinaryNameSpace class. ```ruby def get(index, null_on_oob: false) super end ``` -------------------------------- ### Polars::SQLContext#initialize Source: https://www.rubydoc.info/gems/polars-df/Polars/SQLContext Initializes a new SQLContext instance. ```APIDOC ## SQLContext#initialize ### Description Initializes a new SQLContext instance to manage SQL queries against registered frames. ### Parameters - **frames** (Hash) - Optional - A hash of names to frames. - **register_globals** (Boolean) - Optional - Whether to register global frames. - **eager** (Boolean) - Optional - Whether to default to eager execution. - **named_frames** (Hash) - Optional - Additional frames passed as keyword arguments. ``` -------------------------------- ### Initialize QueryOptFlags Source: https://www.rubydoc.info/gems/polars-df/Polars/QueryOptFlags%3Ainitialize Creates a new QueryOptFlags instance by setting default optimization flags and applying provided overrides. ```ruby def initialize( predicate_pushdown: nil, projection_pushdown: nil, simplify_expression: nil, slice_pushdown: nil, comm_subplan_elim: nil, comm_subexpr_elim: nil, cluster_with_columns: nil, collapse_joins: nil, check_order_observe: nil, fast_projection: nil, sort_collapse: nil ) self._rboptflags = RbOptFlags.default update( predicate_pushdown: predicate_pushdown, projection_pushdown: projection_pushdown, simplify_expression: simplify_expression, slice_pushdown: slice_pushdown, comm_subplan_elim: comm_subplan_elim, comm_subexpr_elim: comm_subexpr_elim, cluster_with_columns: cluster_with_columns, collapse_joins: collapse_joins, check_order_observe: check_order_observe, fast_projection: fast_projection, sort_collapse: sort_collapse ) end ``` -------------------------------- ### Source code for Polars::ArrayNameSpace#get Source: https://www.rubydoc.info/gems/polars-df/Polars/ArrayNameSpace%3Aget The implementation of the get method within the Polars library. ```ruby # File 'lib/polars/array_name_space.rb', line 532 def get(index, null_on_oob: false) super end ``` -------------------------------- ### sample Source: https://www.rubydoc.info/gems/polars-df/Polars/ListNameSpace Sample from this list. ```APIDOC ## sample ### Description Sample from this list. ### Parameters - **n** (Integer) - Optional - Number of items to return. Cannot be used with fraction. - **fraction** (Float) - Optional - Fraction of items to return. Cannot be used with n. - **with_replacement** (Boolean) - Optional - Allow values to be sampled more than once. - **shuffle** (Boolean) - Optional - Shuffle the order of sampled data points. - **seed** (Integer) - Optional - Seed for the random number generator. ### Returns - **Series** ``` -------------------------------- ### Sample from list Source: https://www.rubydoc.info/gems/polars-df/Polars/ListNameSpace Samples items from the list based on provided parameters like n or fraction. ```ruby s = Polars::Series.new("values", [[1, 2, 3], [4, 5]]) s.list.sample(n: Polars::Series.new("n", [2, 1]), seed: 1) ``` ```ruby # File 'lib/polars/list_name_space.rb', line 143 def sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) super end ``` -------------------------------- ### get(index, null_on_oob: false) Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryNameSpace Get the byte value at the given index for every binary value in the Series. ```APIDOC ## get(index, null_on_oob: false) ### Description Get the byte value at the given index. Index 0 returns the first byte, and -1 returns the last byte. ### Parameters - **index** (Object) - Required - Index to return per binary value. - **null_on_oob** (Boolean) - Optional (default: false) - Behavior if index is out of bounds: true sets as null, false raises an error. ### Returns - **Series** - A Series containing the retrieved bytes. ``` -------------------------------- ### Demonstrating Series chunking behavior Source: https://www.rubydoc.info/gems/polars-df/Polars/Series%3Arechunk Examples showing how to check the number of chunks in a Series and how rechunk consolidates them. ```ruby s1 = Polars::Series.new("a", [1, 2, 3]) s1.n_chunks # => 1 ``` ```ruby s2 = Polars::Series.new("a", [4, 5, 6]) s = Polars.concat([s1, s2], rechunk: false) s.n_chunks # => 2 ``` ```ruby s.rechunk(in_place: true) # => # shape: (6,) # Series: 'a' [i64] # [ # 1 # 2 # 3 # 4 # 5 # 6 # ] ``` ```ruby s.n_chunks # => 1 ``` -------------------------------- ### Check if Categorical starts with a prefix Source: https://www.rubydoc.info/gems/polars-df/Polars/CatNameSpace Checks if the string representation of each value in a Categorical Series starts with the specified prefix. ```ruby s = Polars::Series.new("fruits", ["apple", "mango", nil], dtype: Polars::Categorical) s.cat.starts_with("app") # => # shape: (3,) # Series: 'fruits' [bool] # [ # true # false # null # ] ``` ```ruby # File 'lib/polars/cat_name_space.rb', line 124 def starts_with(prefix) super end ``` -------------------------------- ### Check binary prefix with starts_with Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryNameSpace%3Astarts_with Demonstrates checking if elements in a binary series start with a specific byte sequence. ```ruby s = Polars::Series.new("colors", ["\x00\x00\x00".b, "\xff\xff\x00".b, "\x00\x00\xff".b]) s.bin.starts_with("\x00".b) # => # shape: (3,) # Series: 'colors' [bool] # [ # true # false # true # ] ``` -------------------------------- ### get(index, null_on_oob: false) Source: https://www.rubydoc.info/gems/polars-df/Polars/ListExpr Get the value by index in the sublists. If an index is out of bounds, it returns nil or raises an error based on the null_on_oob parameter. ```APIDOC ## get(index, null_on_oob: false) ### Description Get the value by index in the sublists. Index 0 returns the first item, -1 returns the last item. ### Parameters - **index** (Integer) - Required - Index to return per sublist. - **null_on_oob** (Boolean) - Optional (default: false) - Behavior if an index is out of bounds: true sets as null, false raises an error. ### Returns - **Expr** - The resulting expression. ``` -------------------------------- ### Retrieve values from sub-arrays using ArrayExpr#get Source: https://www.rubydoc.info/gems/polars-df/Polars/ArrayExpr%3Aget Demonstrates using the get method to extract elements from an array column based on an index column, with null_on_oob enabled to handle out-of-bounds indices. ```ruby df = Polars::DataFrame.new( {"arr" => [[1, 2, 3], [4, 5, 6], [7, 8, 9]], "idx" => [1, -2, 4]}, schema: {"arr" => Polars::Array.new(Polars::Int32, 3), "idx" => Polars::Int32} ) df.with_columns(get: Polars.col("arr").arr.get("idx", null_on_oob: true)) ``` -------------------------------- ### BinaryNameSpace#starts_with implementation Source: https://www.rubydoc.info/gems/polars-df/Polars/BinaryNameSpace%3Astarts_with The source code definition for the starts_with method within the binary namespace. ```ruby def starts_with(prefix) super end ``` -------------------------------- ### Polars.linear_spaces(start, stop, num_samples, closed: "both", as_array: false, eager: false) Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions%3Alinear_spaces Generates a sequence of evenly-spaced values for each row between start and end. The number of values in each sequence is determined by num_samples. ```APIDOC ## Polars.linear_spaces ### Description Generates a sequence of evenly-spaced values for each row between `start` and `end`. The number of values in each sequence is determined by `num_samples`. ### Parameters - **start** (Object) - Required - Lower bound of the range. - **stop** (Object) - Required - Upper bound of the range. - **num_samples** (Integer) - Required - Number of samples in the output sequence. - **closed** ('both', 'left', 'right', 'none') - Optional (default: "both") - Define which sides of the interval are closed (inclusive). - **as_array** (Boolean) - Optional (default: false) - Return result as a fixed-length Array. num_samples must be a constant. - **eager** (Boolean) - Optional (default: false) - Evaluate immediately and return a Series. If set to false, return an expression instead. ### Returns - **Expr, Series** - Returns an expression or a Series depending on the eager parameter. ``` -------------------------------- ### Implementation of starts_with Source: https://www.rubydoc.info/gems/polars-df/Polars/CatExpr%3Astarts_with The source code implementation for the starts_with method, enforcing a string type check on the prefix. ```ruby def starts_with(prefix) if !prefix.is_a?(::String) msg = "'prefix' must be a string; found #{prefix.inspect}" raise TypeError, msg end Utils.wrap_expr(_rbexpr.cat_starts_with(prefix)) end ``` -------------------------------- ### Select columns and expressions in Polars Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Aselect Examples demonstrating column selection, expression arithmetic, and conditional logic using select. ```ruby df = Polars::DataFrame.new( { "foo" => [1, 2, 3], "bar" => [6, 7, 8], "ham" => ["a", "b", "c"], } ).lazy df.select("foo").collect ``` ```ruby df.select(["foo", "bar"]).collect ``` ```ruby df.select(Polars.col("foo") + 1).collect ``` ```ruby df.select([Polars.col("foo") + 1, Polars.col("bar") + 1]).collect ``` ```ruby df.select(Polars.when(Polars.col("foo") > 2).then(10).otherwise(0)).collect ``` -------------------------------- ### Polars.business_day_count(start, stop, week_mask: [true, true, true, true, true, false, false], holidays: []) Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions%3Abusiness_day_count Calculates the number of business days between start and stop dates. Note: This functionality is considered unstable. ```APIDOC ## Polars.business_day_count ### Description Counts the number of business days between `start` and `stop` (not including `stop`). ### Parameters - **start** (Object) - Required - Start dates. - **stop** (Object) - Required - End dates. - **week_mask** (Array) - Optional - Which days of the week to count. Defaults to [true, true, true, true, true, false, false] (Monday to Friday). - **holidays** (Array) - Optional - Holidays to exclude from the count. Defaults to []. ### Returns - **Expr** - A Polars expression representing the business day count. ``` -------------------------------- ### Rolling Aggregation Example Source: https://www.rubydoc.info/gems/polars-df/Polars/Expr%3Arolling Demonstrates applying rolling sum, min, and max aggregations over a 2-day window on a datetime column. ```ruby dates = [ "2020-01-01 13:45:48", "2020-01-01 16:42:13", "2020-01-01 16:45:09", "2020-01-02 18:12:48", "2020-01-03 19:45:32", "2020-01-08 23:16:43" ] df = Polars::DataFrame.new({"dt" => dates, "a": [3, 7, 5, 9, 2, 1]}).with_columns( Polars.col("dt").str.strptime(Polars::Datetime).set_sorted ) df.with_columns( sum_a: Polars.sum("a").rolling(index_column: "dt", period: "2d"), min_a: Polars.min("a").rolling(index_column: "dt", period: "2d"), max_a: Polars.max("a").rolling(index_column: "dt", period: "2d") ) ``` -------------------------------- ### slice Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get a slice of this Series. ```APIDOC ## slice(offset, length = nil) ### Description Get a slice of this Series. ### Parameters - **offset** (Integer) - Required - Start index. Negative indexing is supported. - **length** (Integer, nil) - Optional - Length of the slice. If set to nil, all rows starting at the offset will be selected. Defaults to nil. ### Returns - **Series** - The sliced Series. ``` -------------------------------- ### Performing Joins on LazyFrames Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Ajoin Examples demonstrating various join strategies including inner, full, left, semi, and anti joins. ```ruby df = Polars::DataFrame.new( { "foo" => [1, 2, 3], "bar" => [6.0, 7.0, 8.0], "ham" => ["a", "b", "c"] } ).lazy other_df = Polars::DataFrame.new( { "apple" => ["x", "y", "z"], "ham" => ["a", "b", "d"] } ).lazy df.join(other_df, on: "ham").collect ``` ```ruby df.join(other_df, on: "ham", how: "full").collect ``` ```ruby df.join(other_df, on: "ham", how: "left").collect ``` ```ruby df.join(other_df, on: "ham", how: "semi").collect ``` ```ruby df.join(other_df, on: "ham", how: "anti").collect ``` -------------------------------- ### name Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the name of this Series. ```APIDOC ## name ### Description Get the name of this Series. ### Returns - String ``` -------------------------------- ### Retrieve Schema Column Names Source: https://www.rubydoc.info/gems/polars-df/Polars/Schema%3Anames Demonstrates how to initialize a schema and extract its column names as an array. ```ruby s = Polars::Schema.new({"x" => Polars::Float64.new, "y" => Polars::Datetime.new(time_zone: "UTC")}) s.names # => ["x", "y"] ``` -------------------------------- ### median Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the median of this Series. ```APIDOC ## median ### Description Get the median of this Series. ### Returns - Float or nil ``` -------------------------------- ### to_dummies Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get dummy variables from the Series. ```APIDOC ## to_dummies ### Description Get dummy variables. ### Parameters - **separator** (String) - Optional - Separator/delimiter used when generating column names. Defaults to "_". - **drop_first** (Boolean) - Optional - Remove the first category from the variable being encoded. Defaults to false. - **drop_nulls** (Boolean) - Optional - If there are nil values in the series, a null column is not generated. Defaults to false. ### Returns - **DataFrame** ``` -------------------------------- ### sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) Source: https://www.rubydoc.info/gems/polars-df/Polars/ListExpr Samples items from a list expression. You can specify either the number of items (n) or a fraction of the total items. ```APIDOC ## sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) ### Description Sample from this list expression. ### Parameters - **n** (Integer) - Optional - Number of items to return. Cannot be used with fraction. - **fraction** (Float) - Optional - Fraction of items to return. Cannot be used with n. - **with_replacement** (Boolean) - Optional - Allow values to be sampled more than once. Defaults to false. - **shuffle** (Boolean) - Optional - Shuffle the order of sampled data points. Defaults to false. - **seed** (Integer) - Optional - Seed for the random number generator. ### Returns - **Expr** - The resulting expression. ``` -------------------------------- ### min Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the minimal value in this Series. ```APIDOC ## min ### Description Get the minimal value in this Series. ### Returns - Object ``` -------------------------------- ### last Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the last element of the Series. ```APIDOC ## last ### Description Get the last element of the Series. Returns nil if the Series is empty. ### Parameters - **ignore_nulls** (Boolean) - Optional - Ignore null values (default: false). ### Returns - **Object** ``` -------------------------------- ### sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Samples elements from the Series. ```APIDOC ## sample(n: nil, fraction: nil, with_replacement: false, shuffle: false, seed: nil) ### Description Sample from this Series. ### Parameters - **n** (Integer, optional) - Optional - Number of items to sample. - **fraction** (Float, optional) - Optional - Fraction of items to sample. - **with_replacement** (Boolean, optional) - Optional - Whether to sample with replacement. - **shuffle** (Boolean, optional) - Optional - Whether to shuffle the result. - **seed** (Integer, optional) - Optional - Random seed. ``` -------------------------------- ### Get Schema Length Source: https://www.rubydoc.info/gems/polars-df/Polars/Schema%3Alength Demonstrates how to initialize a schema and retrieve the number of entries it contains. ```ruby s = Polars::Schema.new({"x" => Polars::Int32.new, "y" => Polars::List.new(Polars::String)}) s.length # => 2 ``` -------------------------------- ### is_unique Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get mask of all unique values. ```APIDOC ## is_unique ### Description Get mask of all unique values. ### Returns - **Series** ``` -------------------------------- ### flags Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get flags that are set on the Series. ```APIDOC ## flags ### Description Get flags that are set on the Series, such as sorting status or fast explode capability. ### Returns - (Hash) A hash containing the flag names and their boolean values. ``` -------------------------------- ### Method implementation Source: https://www.rubydoc.info/gems/polars-df/Polars/DateTimeExpr%3Aminute The source code implementation of the minute method. ```ruby # File 'lib/polars/date_time_expr.rb', line 1098 def minute Utils.wrap_expr(_rbexpr.dt_minute) end ``` -------------------------------- ### arg_unique Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get unique index as Series. ```APIDOC ## Series#arg_unique ### Description Get unique index as Series. ### Returns - **Series** ``` -------------------------------- ### max Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the maximum value in the Series. ```APIDOC ## Series#max ### Description Returns the maximum value found in the Series. ### Returns - **Object** ``` -------------------------------- ### fields Source: https://www.rubydoc.info/gems/polars-df/Polars/StructNameSpace Get the names of the fields in the struct. ```APIDOC ## fields() ### Description Get the names of the fields. ### Returns - **Array** - A list of field names. ``` -------------------------------- ### len() Source: https://www.rubydoc.info/gems/polars-df/Polars/ListNameSpace Get the length of the arrays as UInt32. ```APIDOC ## len() ### Description Get the length of the arrays as UInt32. ### Returns - **Series** - The resulting Series. ``` -------------------------------- ### Rolling Datetimes to Month Start Source: https://www.rubydoc.info/gems/polars-df/Polars/DateTimeNameSpace%3Amonth_start Demonstrates using month_start to adjust a range of datetimes to the first day of each month. ```ruby s = Polars.datetime_range( DateTime.new(2000, 1, 2, 2), DateTime.new(2000, 4, 2, 2), "1mo", time_unit: "us", eager: true ).alias("datetime") s.dt.month_start # => # shape: (4,) # Series: 'datetime' [datetime[μs]] # [ # 2000-01-01 02:00:00 # 2000-02-01 02:00:00 # 2000-03-01 02:00:00 # 2000-04-01 02:00:00 # ] ``` -------------------------------- ### first() Source: https://www.rubydoc.info/gems/polars-df/Polars/ListExpr Get the first value of the sublists. ```APIDOC ## first() ### Description Get the first value of the sublists. ### Returns - (Expr) ``` -------------------------------- ### Clear DataFrame Examples Source: https://www.rubydoc.info/gems/polars-df/Polars/DataFrame%3Aclear Demonstrates creating an empty DataFrame and a DataFrame with a specified number of null rows. ```ruby df = Polars::DataFrame.new( { "a" => [nil, 2, 3, 4], "b" => [0.5, nil, 2.5, 13], "c" => [true, true, false, nil] } ) df.clear # => # shape: (0, 3) # ┌─────┬─────┬──────┐ # │ a ┆ b ┆ c │ # │ --- ┆ --- ┆ --- │ # │ i64 ┆ f64 ┆ bool │ # ╞═════╪═════╪══════╡ # └─────┴─────┴──────┘ ``` ```ruby df.clear(2) # => # shape: (2, 3) # ┌──────┬──────┬──────┐ # │ a ┆ b ┆ c │ # │ --- ┆ --- ┆ --- │ # │ i64 ┆ f64 ┆ bool │ # ╞══════╪══════╪══════╡ # │ null ┆ null ┆ null │ # │ null ┆ null ┆ null │ # └──────┴──────┴──────┘ ``` -------------------------------- ### first() Source: https://www.rubydoc.info/gems/polars-df/Polars/ListNameSpace Get the first value of the sublists. ```APIDOC ## first() ### Description Get the first value of the sublists. ### Returns - **Series** - A Series containing the first elements. ``` -------------------------------- ### Visualize a LazyFrame query plan Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Ashow_graph Demonstrates how to chain operations on a LazyFrame and visualize the resulting query plan using show_graph. ```ruby lf = Polars::LazyFrame.new( { "a" => ["a", "b", "a", "b", "b", "c"], "b" => [1, 2, 3, 4, 5, 6], "c" => [6, 5, 4, 3, 2, 1] } ) lf.group_by("a", maintain_order: true).agg(Polars.all.sum).sort( "a" ).show_graph ``` -------------------------------- ### Initialize Polars Catalog Client Source: https://www.rubydoc.info/gems/polars-df/Polars/Catalog%3Ainitialize Initializes a new catalog client instance. Note that this functionality is currently marked as unstable. ```ruby def initialize(workspace_url, bearer_token: "auto", require_https: true) if require_https && !workspace_url.start_with?("https://") msg = ( "a non-HTTPS workspace_url was given. To " + "allow non-HTTPS URLs, pass require_https: false." ) raise ArgumentError, msg end if bearer_token == "auto" bearer_token = nil end @client = RbCatalogClient.new(workspace_url, bearer_token) end ``` -------------------------------- ### [](item) Source: https://www.rubydoc.info/gems/polars-df/Polars/ListNameSpace Get the value by index in the sublists. ```APIDOC ## [](item) ### Description Get the value by index in the sublists. ### Parameters - **item** (Object) - Required - The index to retrieve from the sublists. ### Returns - **Series** - The resulting Series. ``` -------------------------------- ### Rolling aggregation example Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Arolling Demonstrates how to perform a rolling aggregation on a datetime column with a 2-day window. ```ruby dates = [ "2020-01-01 13:45:48", "2020-01-01 16:42:13", "2020-01-01 16:45:09", "2020-01-02 18:12:48", "2020-01-03 19:45:32", "2020-01-08 23:16:43" ] df = Polars::LazyFrame.new({"dt" => dates, "a" => [3, 7, 5, 9, 2, 1]}).with_columns( Polars.col("dt").str.strptime(Polars::Datetime).set_sorted ) df.rolling(index_column: "dt", period: "2d").agg( [ Polars.sum("a").alias("sum_a"), Polars.min("a").alias("min_a"), Polars.max("a").alias("max_a") ] ).collect ``` -------------------------------- ### [](item) Source: https://www.rubydoc.info/gems/polars-df/Polars/ListExpr Get the value by index in the sublists. ```APIDOC ## [](item) ### Description Get the value by index in the sublists. ### Parameters - **item** (Object) - Required - The index to retrieve. ### Returns - **Expr** - The expression representing the indexed value. ``` -------------------------------- ### Initialize DataFrame for Polars::Functions#exclude Source: https://www.rubydoc.info/gems/polars-df/Polars/Functions%3Aexclude Setup a sample DataFrame to demonstrate column exclusion. ```ruby df = Polars::DataFrame.new( { "aa" => [1, 2, 3], "ba" => ["a", "b", nil], "cc" => [nil, 2.5, 1.5] } ) ``` -------------------------------- ### last Source: https://www.rubydoc.info/gems/polars-df/Polars/ArrayExpr Get the last value of the sub-arrays. ```APIDOC ## last ### Description Returns the last value of each sub-array in the column. ### Returns - **Expr** - A Polars expression representing the last element of each array. ``` -------------------------------- ### get Source: https://www.rubydoc.info/gems/polars-df/Polars/ArrayExpr Retrieves a value by index from sub-arrays. ```APIDOC ## get(index, null_on_oob: false) ### Description Get the value by index in the sub-arrays. Index 0 returns the first item, and negative indices return items from the end. ### Parameters - **index** (Integer) - Required - Index to return per sub-array. - **null_on_oob** (Boolean) - Optional (default: false) - Behavior if an index is out of bounds: true sets as null, false raises an error. ### Returns - **Expr** - The resulting expression. ``` -------------------------------- ### Implementation of the profile method Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Aprofile The internal implementation of the profile method, showing how it handles engine selection and optimization flags. ```ruby def profile( engine: "auto", optimizations: DEFAULT_QUERY_OPT_FLAGS ) engine = _select_engine(engine) ldf = _ldf.with_optimizations(optimizations._rboptflags) df_rb, timings_rb = ldf.profile [Utils.wrap_df(df_rb), Utils.wrap_df(timings_rb)] end ``` -------------------------------- ### n_chunks Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the number of chunks that this Series contains. ```APIDOC ## n_chunks ### Description Get the number of chunks that this Series contains. ### Returns - Integer ``` -------------------------------- ### Implementation of show_graph Source: https://www.rubydoc.info/gems/polars-df/Polars/LazyFrame%3Ashow_graph The source code implementation of the show_graph method, showing how it handles engine selection, plan stages, and dot graph generation. ```ruby def show_graph( optimized: true, show: true, output_path: nil, raw_output: false, figsize: [16.0, 12.0], engine: "auto", plan_stage: "ir", optimizations: DEFAULT_QUERY_OPT_FLAGS ) engine = _select_engine(engine) if engine == "streaming" issue_unstable_warning("streaming mode is considered unstable.") end optimizations = optimizations.dup optimizations._rboptflags.streaming = engine == "streaming" _ldf = self._ldf.with_optimizations(optimizations._rboptflags) if plan_stage == "ir" dot = _ldf.to_dot(optimized) elsif plan_stage == "physical" if engine == "streaming" dot = _ldf.to_dot_streaming_phys(optimized) else dot = _ldf.to_dot(optimized) end else error_msg = "invalid plan stage '#{plan_stage}'" raise TypeError, error_msg end Utils.display_dot_graph( dot: dot, show: show, output_path: output_path, raw_output: raw_output ) end ``` -------------------------------- ### limit Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the first n rows of the Series. ```APIDOC ## limit(n = 10) ### Description Get the first n rows. Alias for #head. ### Parameters - **n** (Integer) - Optional - Number of rows to return. Defaults to 10. ### Returns - **Series** - A new Series containing the first n rows. ``` -------------------------------- ### Retrieve DataFrame Schema Source: https://www.rubydoc.info/gems/polars-df/Polars/DataFrame%3Aschema Demonstrates how to initialize a DataFrame and retrieve its schema definition. ```ruby df = Polars::DataFrame.new( { "foo" => [1, 2, 3], "bar" => [6.0, 7.0, 8.0], "ham" => ["a", "b", "c"] } ) df.schema # => Polars::Schema({"foo"=>Polars::Int64, "bar"=>Polars::Float64, "ham"=>Polars::String}) ``` -------------------------------- ### is_first_distinct() Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get a mask of the first unique value. ```APIDOC ## is_first_distinct() ### Description Get a mask of the first unique value. ### Returns - (Series) ``` -------------------------------- ### create_catalog(catalog_name, comment: nil, storage_root: nil) Source: https://www.rubydoc.info/gems/polars-df/Polars/Catalog Creates a new catalog in the Unity Catalog. This functionality is considered unstable. ```APIDOC ## create_catalog(catalog_name, comment: nil, storage_root: nil) ### Description Create a catalog. This functionality is considered unstable. ### Parameters - **catalog_name** (String) - Required - Name of the catalog. - **comment** (String) - Optional - Leaves a comment about the catalog. - **storage_root** (String) - Optional - Base location at which to store the catalog. ### Returns - **CatalogInfo** ``` -------------------------------- ### get_chunks Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the chunks of this Series as a list of Series. ```APIDOC ## get_chunks ### Description Get the chunks of this Series as a list of Series. ### Returns - (Array) An array of Series objects. ``` -------------------------------- ### std(ddof: 1) Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Get the standard deviation of this Series. ```APIDOC ## std(ddof: 1) ### Description Get the standard deviation of this Series. ### Parameters - **ddof** (Integer) - Optional - Delta Degrees of Freedom: the divisor used in the calculation is N - ddof (defaults to 1). ### Returns - **Float, nil** ``` -------------------------------- ### Get the shape of a Series Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Returns the dimensions of the Series as an array. ```ruby s = Polars::Series.new("a", [1, 2, 3]) s.shape # => [3] ``` ```ruby # File 'lib/polars/series.rb', line 139 def shape [_s.len] end ``` -------------------------------- ### Hugging Face Token Configuration Source: https://www.rubydoc.info/gems/polars-df/Polars/IO Example of passing an API token for Hugging Face cloud storage access. ```python {'token': '...'} ``` -------------------------------- ### Initialize eager optimization flags Source: https://www.rubydoc.info/gems/polars-df/Polars/QueryOptFlags._eager Creates a new QueryOptFlags instance with eager optimizations enabled and expression simplification turned on. ```ruby def self._eager optflags = QueryOptFlags.new optflags.no_optimizations optflags._rboptflags.eager = true optflags.simplify_expression = true optflags end ``` -------------------------------- ### Get Series name Source: https://www.rubydoc.info/gems/polars-df/Polars/Series Returns the name assigned to the Series. ```ruby s = Polars::Series.new("a", [1, 2, 3]) s.name # => "a" ``` ```ruby # File 'lib/polars/series.rb', line 127 def name _s.name end ```