polars-df 0.15.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +274 -0
- data/Cargo.lock +1465 -867
- data/Cargo.toml +3 -0
- data/LICENSE.txt +2 -2
- data/README.md +87 -37
- data/ext/polars/Cargo.toml +47 -16
- data/ext/polars/src/c_api/allocator.rs +7 -0
- data/ext/polars/src/c_api/mod.rs +1 -0
- data/ext/polars/src/catalog/mod.rs +1 -0
- data/ext/polars/src/catalog/unity.rs +470 -0
- data/ext/polars/src/conversion/any_value.rs +99 -84
- data/ext/polars/src/conversion/categorical.rs +30 -0
- data/ext/polars/src/conversion/chunked_array.rs +71 -62
- data/ext/polars/src/conversion/datetime.rs +63 -0
- data/ext/polars/src/conversion/mod.rs +796 -312
- data/ext/polars/src/dataframe/construction.rs +6 -18
- data/ext/polars/src/dataframe/export.rs +30 -39
- data/ext/polars/src/dataframe/general.rs +294 -362
- data/ext/polars/src/dataframe/io.rs +33 -150
- data/ext/polars/src/dataframe/map.rs +175 -0
- data/ext/polars/src/dataframe/mod.rs +37 -5
- data/ext/polars/src/dataframe/serde.rs +23 -8
- data/ext/polars/src/error.rs +44 -7
- data/ext/polars/src/exceptions.rs +21 -8
- data/ext/polars/src/expr/array.rs +86 -22
- data/ext/polars/src/expr/binary.rs +50 -1
- data/ext/polars/src/expr/bitwise.rs +39 -0
- data/ext/polars/src/expr/categorical.rs +20 -0
- data/ext/polars/src/expr/datatype.rs +51 -0
- data/ext/polars/src/expr/datetime.rs +99 -41
- data/ext/polars/src/expr/extension.rs +12 -0
- data/ext/polars/src/expr/general.rs +252 -128
- data/ext/polars/src/expr/list.rs +56 -60
- data/ext/polars/src/expr/meta.rs +30 -35
- data/ext/polars/src/expr/mod.rs +28 -6
- data/ext/polars/src/expr/name.rs +29 -14
- data/ext/polars/src/expr/rolling.rs +111 -3
- data/ext/polars/src/expr/selector.rs +219 -0
- data/ext/polars/src/expr/serde.rs +28 -0
- data/ext/polars/src/expr/string.rs +118 -20
- data/ext/polars/src/expr/struct.rs +14 -1
- data/ext/polars/src/file.rs +194 -86
- data/ext/polars/src/functions/aggregation.rs +13 -12
- data/ext/polars/src/functions/business.rs +2 -3
- data/ext/polars/src/functions/eager.rs +3 -2
- data/ext/polars/src/functions/io.rs +90 -18
- data/ext/polars/src/functions/lazy.rs +267 -118
- data/ext/polars/src/functions/meta.rs +8 -7
- data/ext/polars/src/functions/misc.rs +1 -1
- data/ext/polars/src/functions/mod.rs +2 -1
- data/ext/polars/src/functions/range.rs +88 -31
- data/ext/polars/src/functions/strings.rs +6 -0
- data/ext/polars/src/functions/utils.rs +8 -0
- data/ext/polars/src/interop/arrow/mod.rs +52 -1
- data/ext/polars/src/interop/arrow/{to_ruby.rs → to_rb.rs} +37 -7
- data/ext/polars/src/interop/arrow/to_rust.rs +43 -0
- data/ext/polars/src/interop/numo/to_numo_df.rs +1 -1
- data/ext/polars/src/interop/numo/to_numo_series.rs +72 -50
- data/ext/polars/src/io/cloud_options.rs +107 -0
- data/ext/polars/src/io/mod.rs +4 -0
- data/ext/polars/src/io/scan_options.rs +113 -0
- data/ext/polars/src/io/sink_options.rs +46 -0
- data/ext/polars/src/io/sink_output.rs +21 -0
- data/ext/polars/src/lazyframe/exitable.rs +39 -0
- data/ext/polars/src/lazyframe/general.rs +846 -368
- data/ext/polars/src/lazyframe/mod.rs +58 -5
- data/ext/polars/src/lazyframe/optflags.rs +59 -0
- data/ext/polars/src/lazyframe/serde.rs +36 -4
- data/ext/polars/src/lazyframe/sink.rs +46 -0
- data/ext/polars/src/lazygroupby.rs +38 -9
- data/ext/polars/src/lib.rs +574 -165
- data/ext/polars/src/map/lazy.rs +44 -74
- data/ext/polars/src/map/mod.rs +18 -254
- data/ext/polars/src/map/series.rs +241 -1087
- data/ext/polars/src/on_startup.rs +192 -9
- data/ext/polars/src/prelude.rs +1 -0
- data/ext/polars/src/rb_modules.rs +10 -57
- data/ext/polars/src/ruby/exceptions.rs +26 -0
- data/ext/polars/src/ruby/gvl.rs +104 -0
- data/ext/polars/src/ruby/lazy.rs +46 -0
- data/ext/polars/src/ruby/mod.rs +11 -0
- data/ext/polars/src/ruby/numo.rs +52 -0
- data/ext/polars/src/ruby/plan_callback.rs +198 -0
- data/ext/polars/src/ruby/rb_modules.rs +16 -0
- data/ext/polars/src/ruby/ruby_convert_registry.rs +51 -0
- data/ext/polars/src/ruby/ruby_function.rs +11 -0
- data/ext/polars/src/ruby/ruby_udf.rs +164 -0
- data/ext/polars/src/ruby/thread.rs +65 -0
- data/ext/polars/src/ruby/utils.rs +39 -0
- data/ext/polars/src/series/aggregation.rs +116 -91
- data/ext/polars/src/series/arithmetic.rs +16 -22
- data/ext/polars/src/series/comparison.rs +101 -222
- data/ext/polars/src/series/construction.rs +80 -70
- data/ext/polars/src/series/export.rs +98 -56
- data/ext/polars/src/series/general.rs +323 -440
- data/ext/polars/src/series/import.rs +22 -5
- data/ext/polars/src/series/map.rs +103 -0
- data/ext/polars/src/series/mod.rs +57 -15
- data/ext/polars/src/series/scatter.rs +139 -82
- data/ext/polars/src/sql.rs +16 -9
- data/ext/polars/src/testing/frame.rs +31 -0
- data/ext/polars/src/testing/mod.rs +5 -0
- data/ext/polars/src/testing/series.rs +31 -0
- data/ext/polars/src/timeout.rs +105 -0
- data/ext/polars/src/utils.rs +105 -4
- data/lib/polars/array_expr.rb +500 -22
- data/lib/polars/array_name_space.rb +384 -10
- data/lib/polars/batched_csv_reader.rb +48 -66
- data/lib/polars/binary_expr.rb +217 -0
- data/lib/polars/binary_name_space.rb +155 -1
- data/lib/polars/cat_expr.rb +224 -0
- data/lib/polars/cat_name_space.rb +132 -32
- data/lib/polars/catalog/unity/catalog_info.rb +20 -0
- data/lib/polars/catalog/unity/column_info.rb +31 -0
- data/lib/polars/catalog/unity/namespace_info.rb +21 -0
- data/lib/polars/catalog/unity/table_info.rb +50 -0
- data/lib/polars/catalog.rb +448 -0
- data/lib/polars/collect_batches.rb +22 -0
- data/lib/polars/config.rb +3 -3
- data/lib/polars/convert.rb +201 -36
- data/lib/polars/data_frame.rb +2851 -1017
- data/lib/polars/data_frame_plot.rb +173 -0
- data/lib/polars/data_type_expr.rb +52 -0
- data/lib/polars/data_type_group.rb +6 -0
- data/lib/polars/data_types.rb +118 -18
- data/lib/polars/date_time_expr.rb +426 -84
- data/lib/polars/date_time_name_space.rb +384 -111
- data/lib/polars/dynamic_group_by.rb +102 -10
- data/lib/polars/exceptions.rb +50 -5
- data/lib/polars/expr.rb +2159 -915
- data/lib/polars/extension_expr.rb +39 -0
- data/lib/polars/extension_name_space.rb +39 -0
- data/lib/polars/functions/aggregation/horizontal.rb +11 -6
- data/lib/polars/functions/aggregation/vertical.rb +2 -3
- data/lib/polars/functions/as_datatype.rb +290 -8
- data/lib/polars/functions/business.rb +95 -0
- data/lib/polars/functions/col.rb +6 -5
- data/lib/polars/functions/datatype.rb +62 -0
- data/lib/polars/functions/eager.rb +426 -24
- data/lib/polars/functions/escape_regex.rb +21 -0
- data/lib/polars/functions/lazy.rb +813 -195
- data/lib/polars/functions/lit.rb +21 -10
- data/lib/polars/functions/range/int_range.rb +74 -2
- data/lib/polars/functions/range/linear_space.rb +195 -0
- data/lib/polars/functions/range/time_range.rb +1 -1
- data/lib/polars/functions/repeat.rb +7 -12
- data/lib/polars/functions/whenthen.rb +2 -2
- data/lib/polars/group_by.rb +188 -58
- data/lib/polars/iceberg_dataset.rb +108 -0
- data/lib/polars/in_process_query.rb +37 -0
- data/lib/polars/io/cloud.rb +18 -0
- data/lib/polars/io/csv.rb +336 -128
- data/lib/polars/io/database.rb +19 -4
- data/lib/polars/io/delta.rb +134 -0
- data/lib/polars/io/iceberg.rb +34 -0
- data/lib/polars/io/ipc.rb +63 -63
- data/lib/polars/io/json.rb +16 -0
- data/lib/polars/io/lines.rb +172 -0
- data/lib/polars/io/ndjson.rb +176 -20
- data/lib/polars/io/parquet.rb +173 -95
- data/lib/polars/io/scan_options.rb +55 -0
- data/lib/polars/io/sink_options.rb +27 -0
- data/lib/polars/io/utils.rb +17 -0
- data/lib/polars/lazy_frame.rb +3017 -622
- data/lib/polars/lazy_group_by.rb +436 -2
- data/lib/polars/list_expr.rb +551 -59
- data/lib/polars/list_name_space.rb +465 -51
- data/lib/polars/meta_expr.rb +146 -24
- data/lib/polars/name_expr.rb +87 -2
- data/lib/polars/query_opt_flags.rb +264 -0
- data/lib/polars/rolling_group_by.rb +90 -5
- data/lib/polars/scan_cast_options.rb +86 -0
- data/lib/polars/schema.rb +128 -0
- data/lib/polars/selector.rb +245 -0
- data/lib/polars/selectors.rb +1048 -201
- data/lib/polars/series.rb +2522 -774
- data/lib/polars/series_plot.rb +72 -0
- data/lib/polars/slice.rb +1 -1
- data/lib/polars/sql_context.rb +13 -6
- data/lib/polars/string_cache.rb +19 -72
- data/lib/polars/string_expr.rb +561 -107
- data/lib/polars/string_name_space.rb +781 -109
- data/lib/polars/struct_expr.rb +139 -18
- data/lib/polars/struct_name_space.rb +19 -1
- data/lib/polars/testing.rb +24 -273
- data/lib/polars/utils/constants.rb +2 -0
- data/lib/polars/utils/construction/data_frame.rb +410 -0
- data/lib/polars/utils/construction/series.rb +350 -0
- data/lib/polars/utils/construction/utils.rb +9 -0
- data/lib/polars/utils/convert.rb +18 -8
- data/lib/polars/utils/deprecation.rb +11 -0
- data/lib/polars/utils/parse.rb +62 -9
- data/lib/polars/utils/reduce_balanced.rb +43 -0
- data/lib/polars/utils/serde.rb +22 -0
- data/lib/polars/utils/unstable.rb +19 -0
- data/lib/polars/utils/various.rb +86 -1
- data/lib/polars/utils.rb +63 -48
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +85 -2
- metadata +80 -28
- data/ext/polars/src/allocator.rs +0 -13
- data/ext/polars/src/batched_csv.rs +0 -138
- data/ext/polars/src/functions/string_cache.rs +0 -25
- data/ext/polars/src/map/dataframe.rs +0 -338
- data/lib/polars/plot.rb +0 -109
data/lib/polars/functions/lit.rb
CHANGED
|
@@ -3,7 +3,23 @@ module Polars
|
|
|
3
3
|
# Return an expression representing a literal value.
|
|
4
4
|
#
|
|
5
5
|
# @return [Expr]
|
|
6
|
-
|
|
6
|
+
#
|
|
7
|
+
# @example Literal scalar values:
|
|
8
|
+
# Polars.lit(1)
|
|
9
|
+
# Polars.lit(5.5)
|
|
10
|
+
# Polars.lit(nil)
|
|
11
|
+
# Polars.lit("foo_bar")
|
|
12
|
+
# Polars.lit(Date.new(2021, 1, 20))
|
|
13
|
+
# Polars.lit(DateTime.new(2023, 3, 31, 10, 30, 45))
|
|
14
|
+
#
|
|
15
|
+
# @example Literal list/Series data (1D):
|
|
16
|
+
# Polars.lit([1, 2, 3])
|
|
17
|
+
# Polars.lit(Polars::Series.new("x", [1, 2, 3]))
|
|
18
|
+
#
|
|
19
|
+
# @example Literal list/Series data (2D):
|
|
20
|
+
# Polars.lit([[1, 2], [3, 4]])
|
|
21
|
+
# Polars.lit(Polars::Series.new("y", [[1, 2], [3, 4]]))
|
|
22
|
+
def lit(value, dtype: nil, allow_object: false)
|
|
7
23
|
if value.is_a?(::Time) || value.is_a?(::DateTime)
|
|
8
24
|
time_unit = dtype&.time_unit || "ns"
|
|
9
25
|
time_zone = dtype.&time_zone
|
|
@@ -16,20 +32,15 @@ module Polars
|
|
|
16
32
|
elsif value.is_a?(::Date)
|
|
17
33
|
return lit(::Time.utc(value.year, value.month, value.day)).cast(Date)
|
|
18
34
|
elsif value.is_a?(Polars::Series)
|
|
19
|
-
name = value.name
|
|
20
35
|
value = value._s
|
|
21
|
-
|
|
22
|
-
if name == ""
|
|
23
|
-
return e
|
|
24
|
-
end
|
|
25
|
-
return e.alias(name)
|
|
36
|
+
return Utils.wrap_expr(Plr.lit(value, allow_object, false))
|
|
26
37
|
elsif (defined?(Numo::NArray) && value.is_a?(Numo::NArray)) || value.is_a?(::Array)
|
|
27
|
-
return lit(Series.new("", value))
|
|
38
|
+
return Utils.wrap_expr(Plr.lit(Series.new("literal", [value.to_a], dtype: dtype)._s, allow_object, true))
|
|
28
39
|
elsif dtype
|
|
29
|
-
return Utils.wrap_expr(Plr.lit(value, allow_object)).cast(dtype)
|
|
40
|
+
return Utils.wrap_expr(Plr.lit(value, allow_object, true)).cast(dtype)
|
|
30
41
|
end
|
|
31
42
|
|
|
32
|
-
Utils.wrap_expr(Plr.lit(value, allow_object))
|
|
43
|
+
Utils.wrap_expr(Plr.lit(value, allow_object, true))
|
|
33
44
|
end
|
|
34
45
|
end
|
|
35
46
|
end
|
|
@@ -12,7 +12,7 @@ module Polars
|
|
|
12
12
|
# @param step [Integer]
|
|
13
13
|
# Step size of the range.
|
|
14
14
|
# @param eager [Boolean]
|
|
15
|
-
# If eager evaluation is `
|
|
15
|
+
# If eager evaluation is `true`, a Series is returned instead of an Expr.
|
|
16
16
|
# @param dtype [Symbol]
|
|
17
17
|
# Apply an explicit integer dtype to the resulting expression (default is `Int64`).
|
|
18
18
|
#
|
|
@@ -28,7 +28,7 @@ module Polars
|
|
|
28
28
|
# # 1
|
|
29
29
|
# # 2
|
|
30
30
|
# # ]
|
|
31
|
-
def int_range(start, stop = nil, step: 1, eager: false, dtype:
|
|
31
|
+
def int_range(start = 0, stop = nil, step: 1, eager: false, dtype: Int64)
|
|
32
32
|
if stop.nil?
|
|
33
33
|
stop = start
|
|
34
34
|
start = 0
|
|
@@ -47,5 +47,77 @@ module Polars
|
|
|
47
47
|
result
|
|
48
48
|
end
|
|
49
49
|
alias_method :arange, :int_range
|
|
50
|
+
|
|
51
|
+
# Generate a range of integers for each row of the input columns.
|
|
52
|
+
#
|
|
53
|
+
# @param start [Integer, Expr, Series]
|
|
54
|
+
# Start of the range (inclusive). Defaults to 0.
|
|
55
|
+
# @param stop [Integer, Expr, Series]
|
|
56
|
+
# End of the range (exclusive). If set to `nil` (default),
|
|
57
|
+
# the value of `start` is used and `start` is set to `0`.
|
|
58
|
+
# @param step [Integer]
|
|
59
|
+
# Step size of the range.
|
|
60
|
+
# @param dtype [Object]
|
|
61
|
+
# Integer data type of the ranges. Defaults to `Int64`.
|
|
62
|
+
# @param eager [Boolean]
|
|
63
|
+
# Evaluate immediately and return a `Series`.
|
|
64
|
+
# If set to `false` (default), return an expression instead.
|
|
65
|
+
#
|
|
66
|
+
# @return [Expr, Series]
|
|
67
|
+
#
|
|
68
|
+
# @example
|
|
69
|
+
# df = Polars::DataFrame.new({"start" => [1, -1], "end" => [3, 2]})
|
|
70
|
+
# df.with_columns(int_range: Polars.int_ranges("start", "end"))
|
|
71
|
+
# # =>
|
|
72
|
+
# # shape: (2, 3)
|
|
73
|
+
# # ┌───────┬─────┬────────────┐
|
|
74
|
+
# # │ start ┆ end ┆ int_range │
|
|
75
|
+
# # │ --- ┆ --- ┆ --- │
|
|
76
|
+
# # │ i64 ┆ i64 ┆ list[i64] │
|
|
77
|
+
# # ╞═══════╪═════╪════════════╡
|
|
78
|
+
# # │ 1 ┆ 3 ┆ [1, 2] │
|
|
79
|
+
# # │ -1 ┆ 2 ┆ [-1, 0, 1] │
|
|
80
|
+
# # └───────┴─────┴────────────┘
|
|
81
|
+
#
|
|
82
|
+
# @example `end` can be omitted for a shorter syntax.
|
|
83
|
+
# df.select("end", int_range: Polars.int_ranges("end"))
|
|
84
|
+
# # =>
|
|
85
|
+
# # shape: (2, 2)
|
|
86
|
+
# # ┌─────┬───────────┐
|
|
87
|
+
# # │ end ┆ int_range │
|
|
88
|
+
# # │ --- ┆ --- │
|
|
89
|
+
# # │ i64 ┆ list[i64] │
|
|
90
|
+
# # ╞═════╪═══════════╡
|
|
91
|
+
# # │ 3 ┆ [0, 1, 2] │
|
|
92
|
+
# # │ 2 ┆ [0, 1] │
|
|
93
|
+
# # └─────┴───────────┘
|
|
94
|
+
def int_ranges(
|
|
95
|
+
start = 0,
|
|
96
|
+
stop = nil,
|
|
97
|
+
step: 1,
|
|
98
|
+
dtype: Int64,
|
|
99
|
+
eager: false
|
|
100
|
+
)
|
|
101
|
+
if stop.nil?
|
|
102
|
+
stop = start
|
|
103
|
+
start = 0
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
dtype_expr = Utils.parse_into_datatype_expr(dtype)
|
|
107
|
+
start_rbexpr = Utils.parse_into_expression(start)
|
|
108
|
+
end_rbexpr = Utils.parse_into_expression(stop)
|
|
109
|
+
step_rbexpr = Utils.parse_into_expression(step)
|
|
110
|
+
result = Utils.wrap_expr(
|
|
111
|
+
Plr.int_ranges(
|
|
112
|
+
start_rbexpr, end_rbexpr, step_rbexpr, dtype_expr._rbdatatype_expr
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if eager
|
|
117
|
+
return F.select(result).to_series
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
result
|
|
121
|
+
end
|
|
50
122
|
end
|
|
51
123
|
end
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
module Polars
|
|
2
|
+
module Functions
|
|
3
|
+
# Create sequence of evenly-spaced points.
|
|
4
|
+
#
|
|
5
|
+
# @param start [Object]
|
|
6
|
+
# Lower bound of the range.
|
|
7
|
+
# @param stop [Object]
|
|
8
|
+
# Upper bound of the range.
|
|
9
|
+
# @param num_samples [Object]
|
|
10
|
+
# Number of samples in the output sequence.
|
|
11
|
+
# @param closed ['both', 'left', 'right', 'none']
|
|
12
|
+
# Define which sides of the interval are closed (inclusive).
|
|
13
|
+
# @param eager [Boolean]
|
|
14
|
+
# Evaluate immediately and return a `Series`.
|
|
15
|
+
# If set to `false` (default), return an expression instead.
|
|
16
|
+
#
|
|
17
|
+
# @return [Object]
|
|
18
|
+
#
|
|
19
|
+
# @note
|
|
20
|
+
# This functionality is experimental. It may be changed at any point without it
|
|
21
|
+
# being considered a breaking change.
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# Polars.linear_space(0, 1, 3, eager: true)
|
|
25
|
+
# # =>
|
|
26
|
+
# # shape: (3,)
|
|
27
|
+
# # Series: 'literal' [f64]
|
|
28
|
+
# # [
|
|
29
|
+
# # 0.0
|
|
30
|
+
# # 0.5
|
|
31
|
+
# # 1.0
|
|
32
|
+
# # ]
|
|
33
|
+
#
|
|
34
|
+
# @example
|
|
35
|
+
# Polars.linear_space(0, 1, 3, closed: "left", eager: true)
|
|
36
|
+
# # =>
|
|
37
|
+
# # shape: (3,)
|
|
38
|
+
# # Series: 'literal' [f64]
|
|
39
|
+
# # [
|
|
40
|
+
# # 0.0
|
|
41
|
+
# # 0.333333
|
|
42
|
+
# # 0.666667
|
|
43
|
+
# # ]
|
|
44
|
+
#
|
|
45
|
+
# @example
|
|
46
|
+
# Polars.linear_space(0, 1, 3, closed: "right", eager: true)
|
|
47
|
+
# # =>
|
|
48
|
+
# # shape: (3,)
|
|
49
|
+
# # Series: 'literal' [f64]
|
|
50
|
+
# # [
|
|
51
|
+
# # 0.333333
|
|
52
|
+
# # 0.666667
|
|
53
|
+
# # 1.0
|
|
54
|
+
# # ]
|
|
55
|
+
#
|
|
56
|
+
# @example
|
|
57
|
+
# Polars.linear_space(0, 1, 3, closed: "none", eager: true)
|
|
58
|
+
# # =>
|
|
59
|
+
# # shape: (3,)
|
|
60
|
+
# # Series: 'literal' [f64]
|
|
61
|
+
# # [
|
|
62
|
+
# # 0.25
|
|
63
|
+
# # 0.5
|
|
64
|
+
# # 0.75
|
|
65
|
+
# # ]
|
|
66
|
+
#
|
|
67
|
+
# @example `Date` endpoints generate a sequence of `Datetime` values:
|
|
68
|
+
# Polars.linear_space(
|
|
69
|
+
# Date.new(2025, 1, 1),
|
|
70
|
+
# Date.new(2025, 2, 1),
|
|
71
|
+
# 3,
|
|
72
|
+
# closed: "right",
|
|
73
|
+
# eager: true
|
|
74
|
+
# )
|
|
75
|
+
# # =>
|
|
76
|
+
# # shape: (3,)
|
|
77
|
+
# # Series: 'literal' [datetime[μs]]
|
|
78
|
+
# # [
|
|
79
|
+
# # 2025-01-11 08:00:00
|
|
80
|
+
# # 2025-01-21 16:00:00
|
|
81
|
+
# # 2025-02-01 00:00:00
|
|
82
|
+
# # ]
|
|
83
|
+
#
|
|
84
|
+
# @example When `eager: false` (default), an expression is produced. You can generate a sequence using the length of the dataframe:
|
|
85
|
+
# df = Polars::DataFrame.new({"a" => [1, 2, 3, 4, 5]})
|
|
86
|
+
# df.with_columns(Polars.linear_space(0, 1, Polars.len).alias("ls"))
|
|
87
|
+
# # =>
|
|
88
|
+
# # shape: (5, 2)
|
|
89
|
+
# # ┌─────┬──────┐
|
|
90
|
+
# # │ a ┆ ls │
|
|
91
|
+
# # │ --- ┆ --- │
|
|
92
|
+
# # │ i64 ┆ f64 │
|
|
93
|
+
# # ╞═════╪══════╡
|
|
94
|
+
# # │ 1 ┆ 0.0 │
|
|
95
|
+
# # │ 2 ┆ 0.25 │
|
|
96
|
+
# # │ 3 ┆ 0.5 │
|
|
97
|
+
# # │ 4 ┆ 0.75 │
|
|
98
|
+
# # │ 5 ┆ 1.0 │
|
|
99
|
+
# # └─────┴──────┘
|
|
100
|
+
def linear_space(
|
|
101
|
+
start,
|
|
102
|
+
stop,
|
|
103
|
+
num_samples,
|
|
104
|
+
closed: "both",
|
|
105
|
+
eager: false
|
|
106
|
+
)
|
|
107
|
+
start_rbexpr = Utils.parse_into_expression(start)
|
|
108
|
+
end_rbexpr = Utils.parse_into_expression(stop)
|
|
109
|
+
num_samples_rbexpr = Utils.parse_into_expression(num_samples)
|
|
110
|
+
result = Utils.wrap_expr(
|
|
111
|
+
Plr.linear_space(start_rbexpr, end_rbexpr, num_samples_rbexpr, closed)
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
if eager
|
|
115
|
+
return F.select(result).to_series
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
result
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Generate a sequence of evenly-spaced values for each row between `start` and `end`.
|
|
122
|
+
#
|
|
123
|
+
# The number of values in each sequence is determined by `num_samples`.
|
|
124
|
+
#
|
|
125
|
+
# @param start [Object]
|
|
126
|
+
# Lower bound of the range.
|
|
127
|
+
# @param stop [Object]
|
|
128
|
+
# Upper bound of the range.
|
|
129
|
+
# @param num_samples [Integer]
|
|
130
|
+
# Number of samples in the output sequence.
|
|
131
|
+
# @param closed ['both', 'left', 'right', 'none']
|
|
132
|
+
# Define which sides of the interval are closed (inclusive).
|
|
133
|
+
# @param as_array [Boolean]
|
|
134
|
+
# Return result as a fixed-length `Array`. `num_samples` must be a constant.
|
|
135
|
+
# @param eager [Boolean]
|
|
136
|
+
# Evaluate immediately and return a `Series`.
|
|
137
|
+
# If set to `false` (default), return an expression instead.
|
|
138
|
+
#
|
|
139
|
+
# @return [Expr, Series]
|
|
140
|
+
#
|
|
141
|
+
# @note
|
|
142
|
+
# This functionality is experimental. It may be changed at any point without it
|
|
143
|
+
# being considered a breaking change.
|
|
144
|
+
#
|
|
145
|
+
# @example
|
|
146
|
+
# df = Polars::DataFrame.new({"start" => [1, -1], "end" => [3, 2], "num_samples" => [4, 5]})
|
|
147
|
+
# df.with_columns(ls: Polars.linear_spaces("start", "end", "num_samples"))
|
|
148
|
+
# # =>
|
|
149
|
+
# # shape: (2, 4)
|
|
150
|
+
# # ┌───────┬─────┬─────────────┬────────────────────────┐
|
|
151
|
+
# # │ start ┆ end ┆ num_samples ┆ ls │
|
|
152
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
|
153
|
+
# # │ i64 ┆ i64 ┆ i64 ┆ list[f64] │
|
|
154
|
+
# # ╞═══════╪═════╪═════════════╪════════════════════════╡
|
|
155
|
+
# # │ 1 ┆ 3 ┆ 4 ┆ [1.0, 1.666667, … 3.0] │
|
|
156
|
+
# # │ -1 ┆ 2 ┆ 5 ┆ [-1.0, -0.25, … 2.0] │
|
|
157
|
+
# # └───────┴─────┴─────────────┴────────────────────────┘
|
|
158
|
+
#
|
|
159
|
+
# @example
|
|
160
|
+
# df.with_columns(ls: Polars.linear_spaces("start", "end", 3, as_array: true))
|
|
161
|
+
# # =>
|
|
162
|
+
# # shape: (2, 4)
|
|
163
|
+
# # ┌───────┬─────┬─────────────┬──────────────────┐
|
|
164
|
+
# # │ start ┆ end ┆ num_samples ┆ ls │
|
|
165
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
|
166
|
+
# # │ i64 ┆ i64 ┆ i64 ┆ array[f64, 3] │
|
|
167
|
+
# # ╞═══════╪═════╪═════════════╪══════════════════╡
|
|
168
|
+
# # │ 1 ┆ 3 ┆ 4 ┆ [1.0, 2.0, 3.0] │
|
|
169
|
+
# # │ -1 ┆ 2 ┆ 5 ┆ [-1.0, 0.5, 2.0] │
|
|
170
|
+
# # └───────┴─────┴─────────────┴──────────────────┘
|
|
171
|
+
def linear_spaces(
|
|
172
|
+
start,
|
|
173
|
+
stop,
|
|
174
|
+
num_samples,
|
|
175
|
+
closed: "both",
|
|
176
|
+
as_array: false,
|
|
177
|
+
eager: false
|
|
178
|
+
)
|
|
179
|
+
start_rbexpr = Utils.parse_into_expression(start)
|
|
180
|
+
end_rbexpr = Utils.parse_into_expression(stop)
|
|
181
|
+
num_samples_rbexpr = Utils.parse_into_expression(num_samples)
|
|
182
|
+
result = Utils.wrap_expr(
|
|
183
|
+
Plr.linear_spaces(
|
|
184
|
+
start_rbexpr, end_rbexpr, num_samples_rbexpr, closed, as_array
|
|
185
|
+
)
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
if eager
|
|
189
|
+
return F.select(result).to_series
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
result
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -12,7 +12,7 @@ module Polars
|
|
|
12
12
|
# Define which sides of the range are closed (inclusive).
|
|
13
13
|
# @param eager [Boolean]
|
|
14
14
|
# Evaluate immediately and return a `Series`.
|
|
15
|
-
# If set to `
|
|
15
|
+
# If set to `false` (default), return an expression instead.
|
|
16
16
|
#
|
|
17
17
|
# @return [Object]
|
|
18
18
|
#
|
|
@@ -6,10 +6,12 @@ module Polars
|
|
|
6
6
|
# Value to repeat.
|
|
7
7
|
# @param n [Integer]
|
|
8
8
|
# Repeat `n` times.
|
|
9
|
+
# @param dtype [Object]
|
|
10
|
+
# Data type of the resulting column. If set to `nil` (default), data type is
|
|
11
|
+
# inferred from the given value. Defaults to Int32 for integer values, unless
|
|
12
|
+
# Int64 is required to fit the given value. Defaults to Float64 for float values.
|
|
9
13
|
# @param eager [Boolean]
|
|
10
14
|
# Run eagerly and collect into a `Series`.
|
|
11
|
-
# @param name [String]
|
|
12
|
-
# Only used in `eager` mode. As expression, use `alias`.
|
|
13
15
|
#
|
|
14
16
|
# @return [Object]
|
|
15
17
|
#
|
|
@@ -34,20 +36,13 @@ module Polars
|
|
|
34
36
|
# # 3
|
|
35
37
|
# # 3
|
|
36
38
|
# # ]
|
|
37
|
-
def repeat(value, n, dtype: nil, eager: false
|
|
38
|
-
if !name.nil?
|
|
39
|
-
warn "the `name` argument is deprecated. Use the `alias` method instead."
|
|
40
|
-
end
|
|
41
|
-
|
|
39
|
+
def repeat(value, n, dtype: nil, eager: false)
|
|
42
40
|
if n.is_a?(Integer)
|
|
43
41
|
n = lit(n)
|
|
44
42
|
end
|
|
45
43
|
|
|
46
44
|
value = Utils.parse_into_expression(value, str_as_lit: true)
|
|
47
45
|
expr = Utils.wrap_expr(Plr.repeat(value, n._rbexpr, dtype))
|
|
48
|
-
if !name.nil?
|
|
49
|
-
expr = expr.alias(name)
|
|
50
|
-
end
|
|
51
46
|
if eager
|
|
52
47
|
return select(expr).to_series
|
|
53
48
|
end
|
|
@@ -78,7 +73,7 @@ module Polars
|
|
|
78
73
|
# # 1
|
|
79
74
|
# # 1
|
|
80
75
|
# # ]
|
|
81
|
-
def ones(n, dtype:
|
|
76
|
+
def ones(n, dtype: Float64, eager: false)
|
|
82
77
|
if (zero = _one_or_zero_by_dtype(1, dtype)).nil?
|
|
83
78
|
msg = "invalid dtype for `ones`; found #{dtype}"
|
|
84
79
|
raise TypeError, msg
|
|
@@ -111,7 +106,7 @@ module Polars
|
|
|
111
106
|
# # 0
|
|
112
107
|
# # 0
|
|
113
108
|
# # ]
|
|
114
|
-
def zeros(n, dtype:
|
|
109
|
+
def zeros(n, dtype: Float64, eager: false)
|
|
115
110
|
if (zero = _one_or_zero_by_dtype(0, dtype)).nil?
|
|
116
111
|
msg = "invalid dtype for `zeros`; found #{dtype}"
|
|
117
112
|
raise TypeError, msg
|
|
@@ -6,7 +6,7 @@ module Polars
|
|
|
6
6
|
#
|
|
7
7
|
# @example Below we add a column with the value 1, where column "foo" > 2 and the value -1 where it isn't.
|
|
8
8
|
# df = Polars::DataFrame.new({"foo" => [1, 3, 4], "bar" => [3, 4, 0]})
|
|
9
|
-
# df.
|
|
9
|
+
# df.with_columns(Polars.when(Polars.col("foo") > 2).then(Polars.lit(1)).otherwise(Polars.lit(-1)))
|
|
10
10
|
# # =>
|
|
11
11
|
# # shape: (3, 3)
|
|
12
12
|
# # ┌─────┬─────┬─────────┐
|
|
@@ -40,7 +40,7 @@ module Polars
|
|
|
40
40
|
# # │ 4 ┆ 0 ┆ 1 │
|
|
41
41
|
# # └─────┴─────┴─────┘
|
|
42
42
|
#
|
|
43
|
-
# @example The `otherwise` at the end is optional. If left out, any rows where none of the `when` expressions evaluate to
|
|
43
|
+
# @example The `otherwise` at the end is optional. If left out, any rows where none of the `when` expressions evaluate to true, are set to `null`:
|
|
44
44
|
# df.with_columns(Polars.when(Polars.col("foo") > 2).then(1).alias("val"))
|
|
45
45
|
# # =>
|
|
46
46
|
# # shape: (3, 3)
|