Module: Polars::Selectors
- Defined in:
- lib/polars/selectors.rb
Class Method Summary collapse
-
.all ⇒ SelectorProxy
Select all columns.
-
.alpha(ascii_only: false, ignore_spaces: false) ⇒ SelectorProxy
Select all columns with alphabetic names (eg: only letters).
-
.binary ⇒ SelectorProxy
Select all binary columns.
-
.boolean ⇒ SelectorProxy
Select all boolean columns.
-
.by_name(*names, require_all: true) ⇒ SelectorProxy
Select all columns matching the given names.
-
.categorical ⇒ SelectorProxy
Select all categorical columns.
-
.contains(*substring) ⇒ SelectorProxy
Select columns whose names contain the given literal substring(s).
-
.date ⇒ SelectorProxy
Select all date columns.
-
.decimal ⇒ SelectorProxy
Select all decimal columns.
-
.ends_with(*suffix) ⇒ SelectorProxy
Select columns that end with the given substring(s).
-
.first ⇒ SelectorProxy
Select the first column in the current scope.
-
.float ⇒ SelectorProxy
Select all float columns.
-
.integer ⇒ SelectorProxy
Select all integer columns.
-
.last ⇒ SelectorProxy
Select the last column in the current scope.
-
.numeric ⇒ SelectorProxy
Select all numeric columns.
-
.signed_integer ⇒ SelectorProxy
Select all signed integer columns.
-
.starts_with(*prefix) ⇒ SelectorProxy
Select columns that start with the given substring(s).
-
.string(include_categorical: false) ⇒ SelectorProxy
Select all String (and, optionally, Categorical) string columns.
-
.time ⇒ SelectorProxy
Select all time columns.
-
.unsigned_integer ⇒ SelectorProxy
Select all unsigned integer columns.
Class Method Details
.all ⇒ SelectorProxy
Select all columns.
172 173 174 |
# File 'lib/polars/selectors.rb', line 172 def self.all _selector_proxy_(F.all, name: "all") end |
.alpha(ascii_only: false, ignore_spaces: false) ⇒ SelectorProxy
Matching column names cannot contain any non-alphabetic characters. Note
that the definition of “alphabetic” consists of all valid Unicode alphabetic
characters (\p{Alphabetic}
) by default; this can be changed by setting
ascii_only: true
.
Select all columns with alphabetic names (eg: only letters).
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/polars/selectors.rb', line 273 def self.alpha(ascii_only: false, ignore_spaces: false) # note that we need to supply a pattern compatible with the *rust* regex crate re_alpha = ascii_only ? "a-zA-Z" : "\\p{Alphabetic}" re_space = ignore_spaces ? " " : "" _selector_proxy_( F.col("^[#{re_alpha}#{re_space}]+$"), name: "alpha", parameters: {"ascii_only" => ascii_only, "ignore_spaces" => ignore_spaces}, ) end |
.binary ⇒ SelectorProxy
Select all binary columns.
311 312 313 |
# File 'lib/polars/selectors.rb', line 311 def self.binary _selector_proxy_(F.col(Binary), name: "binary") end |
.boolean ⇒ SelectorProxy
Select all boolean columns.
363 364 365 |
# File 'lib/polars/selectors.rb', line 363 def self.boolean _selector_proxy_(F.col(Boolean), name: "boolean") end |
.by_name(*names, require_all: true) ⇒ SelectorProxy
Matching columns are returned in the order in which they are declared in the selector, not the underlying schema order.
Select all columns matching the given names.
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/polars/selectors.rb', line 436 def self.by_name(*names, require_all: true) all_names = [] names.each do |nm| if nm.is_a?(::String) all_names << nm else msg = "invalid name: #{nm.inspect}" raise TypeError, msg end end selector_params = {"*names" => all_names} match_cols = all_names if !require_all match_cols = "^(#{all_names.map { |nm| Utils.re_escape(nm) }.join("|")})$" selector_params["require_all"] = require_all end _selector_proxy_( F.col(match_cols), name: "by_name", parameters: selector_params ) end |
.categorical ⇒ SelectorProxy
Select all categorical columns.
500 501 502 |
# File 'lib/polars/selectors.rb', line 500 def self.categorical _selector_proxy_(F.col(Categorical), name: "categorical") end |
.contains(*substring) ⇒ SelectorProxy
Select columns whose names contain the given literal substring(s).
559 560 561 562 563 564 565 566 567 568 |
# File 'lib/polars/selectors.rb', line 559 def self.contains(*substring) escaped_substring = _re_string(substring) raw_params = "^.*#{escaped_substring}.*$" _selector_proxy_( F.col(raw_params), name: "contains", parameters: {"*substring" => escaped_substring} ) end |
.date ⇒ SelectorProxy
Select all date columns.
607 608 609 |
# File 'lib/polars/selectors.rb', line 607 def self.date _selector_proxy_(F.col(Date), name: "date") end |
.decimal ⇒ SelectorProxy
Select all decimal columns.
655 656 657 658 |
# File 'lib/polars/selectors.rb', line 655 def self.decimal # TODO: allow explicit selection by scale/precision? _selector_proxy_(F.col(Decimal), name: "decimal") end |
.ends_with(*suffix) ⇒ SelectorProxy
Select columns that end with the given substring(s).
715 716 717 718 719 720 721 722 723 724 |
# File 'lib/polars/selectors.rb', line 715 def self.ends_with(*suffix) escaped_suffix = _re_string(suffix) raw_params = "^.*#{escaped_suffix}$" _selector_proxy_( F.col(raw_params), name: "ends_with", parameters: {"*suffix" => escaped_suffix}, ) end |
.first ⇒ SelectorProxy
Select the first column in the current scope.
765 766 767 |
# File 'lib/polars/selectors.rb', line 765 def self.first _selector_proxy_(F.first, name: "first") end |
.float ⇒ SelectorProxy
Select all float columns.
809 810 811 |
# File 'lib/polars/selectors.rb', line 809 def self.float _selector_proxy_(F.col(FLOAT_DTYPES), name: "float") end |
.integer ⇒ SelectorProxy
Select all integer columns.
852 853 854 |
# File 'lib/polars/selectors.rb', line 852 def self.integer _selector_proxy_(F.col(INTEGER_DTYPES), name: "integer") end |
.last ⇒ SelectorProxy
Select the last column in the current scope.
1009 1010 1011 |
# File 'lib/polars/selectors.rb', line 1009 def self.last _selector_proxy_(F.last, name: "last") end |
.numeric ⇒ SelectorProxy
Select all numeric columns.
1053 1054 1055 |
# File 'lib/polars/selectors.rb', line 1053 def self.numeric _selector_proxy_(F.col(NUMERIC_DTYPES), name: "numeric") end |
.signed_integer ⇒ SelectorProxy
Select all signed integer columns.
909 910 911 |
# File 'lib/polars/selectors.rb', line 909 def self.signed_integer _selector_proxy_(F.col(SIGNED_INTEGER_DTYPES), name: "signed_integer") end |
.starts_with(*prefix) ⇒ SelectorProxy
Select columns that start with the given substring(s).
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 |
# File 'lib/polars/selectors.rb', line 1112 def self.starts_with(*prefix) escaped_prefix = _re_string(prefix) raw_params = "^#{escaped_prefix}.*$" _selector_proxy_( F.col(raw_params), name: "starts_with", parameters: {"*prefix" => prefix} ) end |
.string(include_categorical: false) ⇒ SelectorProxy
Select all String (and, optionally, Categorical) string columns.
df.group_by(Polars.cs.string).agg(Polars.cs.numeric.sum).sort(Polars.cs.string) shape: (2, 3) ┌─────┬─────┬─────┐ │ w ┆ x ┆ y │ │ — ┆ — ┆ — │ │ str ┆ i64 ┆ f64 │ ╞═════╪═════╪═════╡ │ xx ┆ 0 ┆ 2.0 │ │ yy ┆ 6 ┆ 7.0 │ └─────┴─────┴─────┘
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 |
# File 'lib/polars/selectors.rb', line 1166 def self.string(include_categorical: false) string_dtypes = [String] if include_categorical string_dtypes << Categorical end _selector_proxy_( F.col(string_dtypes), name: "string", parameters: {"include_categorical" => include_categorical}, ) end |
.time ⇒ SelectorProxy
Select all time columns.
1218 1219 1220 |
# File 'lib/polars/selectors.rb', line 1218 def self.time _selector_proxy_(F.col(Time), name: "time") end |
.unsigned_integer ⇒ SelectorProxy
Select all unsigned integer columns.
966 967 968 |
# File 'lib/polars/selectors.rb', line 966 def self.unsigned_integer _selector_proxy_(F.col(UNSIGNED_INTEGER_DTYPES), name: "unsigned_integer") end |