polars-df 0.21.0 → 0.21.1

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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/Cargo.lock +1 -1
  4. data/ext/polars/Cargo.toml +7 -1
  5. data/ext/polars/src/conversion/mod.rs +92 -4
  6. data/ext/polars/src/exceptions.rs +1 -0
  7. data/ext/polars/src/expr/array.rs +73 -4
  8. data/ext/polars/src/expr/binary.rs +26 -1
  9. data/ext/polars/src/expr/bitwise.rs +39 -0
  10. data/ext/polars/src/expr/categorical.rs +20 -0
  11. data/ext/polars/src/expr/datatype.rs +24 -1
  12. data/ext/polars/src/expr/datetime.rs +58 -0
  13. data/ext/polars/src/expr/general.rs +84 -5
  14. data/ext/polars/src/expr/list.rs +24 -0
  15. data/ext/polars/src/expr/meta.rs +11 -0
  16. data/ext/polars/src/expr/mod.rs +1 -0
  17. data/ext/polars/src/expr/name.rs +8 -0
  18. data/ext/polars/src/expr/rolling.rs +20 -0
  19. data/ext/polars/src/expr/string.rs +59 -0
  20. data/ext/polars/src/expr/struct.rs +9 -1
  21. data/ext/polars/src/functions/io.rs +19 -0
  22. data/ext/polars/src/functions/lazy.rs +4 -0
  23. data/ext/polars/src/lazyframe/general.rs +51 -0
  24. data/ext/polars/src/lib.rs +119 -10
  25. data/ext/polars/src/map/dataframe.rs +2 -2
  26. data/ext/polars/src/map/series.rs +1 -1
  27. data/ext/polars/src/series/aggregation.rs +44 -0
  28. data/ext/polars/src/series/general.rs +64 -4
  29. data/lib/polars/array_expr.rb +382 -3
  30. data/lib/polars/array_name_space.rb +281 -0
  31. data/lib/polars/binary_expr.rb +67 -0
  32. data/lib/polars/binary_name_space.rb +43 -0
  33. data/lib/polars/cat_expr.rb +224 -0
  34. data/lib/polars/cat_name_space.rb +138 -0
  35. data/lib/polars/config.rb +2 -2
  36. data/lib/polars/convert.rb +6 -6
  37. data/lib/polars/data_frame.rb +684 -19
  38. data/lib/polars/data_type_expr.rb +52 -0
  39. data/lib/polars/data_types.rb +14 -2
  40. data/lib/polars/date_time_expr.rb +251 -0
  41. data/lib/polars/date_time_name_space.rb +299 -0
  42. data/lib/polars/expr.rb +1213 -180
  43. data/lib/polars/functions/datatype.rb +21 -0
  44. data/lib/polars/functions/lazy.rb +13 -0
  45. data/lib/polars/io/csv.rb +1 -1
  46. data/lib/polars/io/json.rb +4 -4
  47. data/lib/polars/io/ndjson.rb +4 -4
  48. data/lib/polars/io/parquet.rb +27 -5
  49. data/lib/polars/lazy_frame.rb +936 -20
  50. data/lib/polars/list_expr.rb +196 -4
  51. data/lib/polars/list_name_space.rb +201 -4
  52. data/lib/polars/meta_expr.rb +64 -0
  53. data/lib/polars/name_expr.rb +36 -0
  54. data/lib/polars/schema.rb +79 -3
  55. data/lib/polars/selector.rb +72 -0
  56. data/lib/polars/selectors.rb +3 -3
  57. data/lib/polars/series.rb +1051 -54
  58. data/lib/polars/string_expr.rb +411 -6
  59. data/lib/polars/string_name_space.rb +722 -49
  60. data/lib/polars/struct_expr.rb +103 -0
  61. data/lib/polars/struct_name_space.rb +19 -1
  62. data/lib/polars/utils/various.rb +18 -1
  63. data/lib/polars/utils.rb +5 -1
  64. data/lib/polars/version.rb +1 -1
  65. data/lib/polars.rb +2 -0
  66. metadata +4 -1
@@ -57,6 +57,36 @@ module Polars
57
57
  Utils.wrap_expr(_rbexpr.struct_field_by_name(name))
58
58
  end
59
59
 
60
+ # Expand the struct into its individual fields.
61
+ #
62
+ # Alias for `Expr.struct.field("*")`.
63
+ #
64
+ # @return [Expr]
65
+ #
66
+ # @example
67
+ # df = Polars::DataFrame.new(
68
+ # {
69
+ # "aaa" => [1, 2],
70
+ # "bbb" => ["ab", "cd"],
71
+ # "ccc" => [true, nil],
72
+ # "ddd" => [[1, 2], [3]]
73
+ # }
74
+ # ).select(Polars.struct("aaa", "bbb", "ccc", "ddd").alias("struct_col"))
75
+ # df.select(Polars.col("struct_col").struct.unnest)
76
+ # # =>
77
+ # # shape: (2, 4)
78
+ # # ┌─────┬─────┬──────┬───────────┐
79
+ # # │ aaa ┆ bbb ┆ ccc ┆ ddd │
80
+ # # │ --- ┆ --- ┆ --- ┆ --- │
81
+ # # │ i64 ┆ str ┆ bool ┆ list[i64] │
82
+ # # ╞═════╪═════╪══════╪═══════════╡
83
+ # # │ 1 ┆ ab ┆ true ┆ [1, 2] │
84
+ # # │ 2 ┆ cd ┆ null ┆ [3] │
85
+ # # └─────┴─────┴──────┴───────────┘
86
+ def unnest
87
+ field("*")
88
+ end
89
+
60
90
  # Rename the fields of the struct.
61
91
  #
62
92
  # @param names [Array]
@@ -94,5 +124,78 @@ module Polars
94
124
  def rename_fields(names)
95
125
  Utils.wrap_expr(_rbexpr.struct_rename_fields(names))
96
126
  end
127
+
128
+ # Convert this struct to a string column with json values.
129
+ #
130
+ # @return [Expr]
131
+ #
132
+ # @example
133
+ # Polars::DataFrame.new(
134
+ # {"a" => [{"a" => [1, 2], "b" => [45]}, {"a" => [9, 1, 3], "b" => nil}]}
135
+ # ).with_columns(Polars.col("a").struct.json_encode.alias("encoded"))
136
+ # # =>
137
+ # # shape: (2, 2)
138
+ # # ┌──────────────────┬────────────────────────┐
139
+ # # │ a ┆ encoded │
140
+ # # │ --- ┆ --- │
141
+ # # │ struct[2] ┆ str │
142
+ # # ╞══════════════════╪════════════════════════╡
143
+ # # │ {[1, 2],[45]} ┆ {"a":[1,2],"b":[45]} │
144
+ # # │ {[9, 1, 3],null} ┆ {"a":[9,1,3],"b":null} │
145
+ # # └──────────────────┴────────────────────────┘
146
+ def json_encode
147
+ Utils.wrap_expr(_rbexpr.struct_json_encode)
148
+ end
149
+
150
+ # Add or overwrite fields of this struct.
151
+ #
152
+ # This is similar to `with_columns` on `DataFrame`.
153
+ #
154
+ # @param exprs [Array]
155
+ # Field(s) to add, specified as positional arguments.
156
+ # Accepts expression input. Strings are parsed as column names, other
157
+ # non-expression inputs are parsed as literals.
158
+ # @param named_exprs [Hash]
159
+ # Additional fields to add, specified as keyword arguments.
160
+ # The columns will be renamed to the keyword used.
161
+ #
162
+ # @return [Expr]
163
+ #
164
+ # @example
165
+ # df = Polars::DataFrame.new(
166
+ # {
167
+ # "coords" => [{"x" => 1, "y" => 4}, {"x" => 4, "y" => 9}, {"x" => 9, "y" => 16}],
168
+ # "multiply" => [10, 2, 3]
169
+ # }
170
+ # )
171
+ # df.with_columns(
172
+ # Polars.col("coords").struct.with_fields(
173
+ # Polars.field("x").sqrt,
174
+ # y_mul: Polars.field("y") * Polars.col("multiply")
175
+ # )
176
+ # )
177
+ # # =>
178
+ # # shape: (3, 2)
179
+ # # ┌─────────────┬──────────┐
180
+ # # │ coords ┆ multiply │
181
+ # # │ --- ┆ --- │
182
+ # # │ struct[3] ┆ i64 │
183
+ # # ╞═════════════╪══════════╡
184
+ # # │ {1.0,4,40} ┆ 10 │
185
+ # # │ {2.0,9,18} ┆ 2 │
186
+ # # │ {3.0,16,48} ┆ 3 │
187
+ # # └─────────────┴──────────┘
188
+ def with_fields(
189
+ *exprs,
190
+ **named_exprs
191
+ )
192
+ structify = ENV.fetch("POLARS_AUTO_STRUCTIFY", 0).to_i != 0
193
+
194
+ rbexprs = Utils.parse_into_list_of_expressions(
195
+ *exprs, **named_exprs, __structify: structify
196
+ )
197
+
198
+ Utils.wrap_expr(_rbexpr.struct_with_fields(rbexprs))
199
+ end
97
200
  end
98
201
  end
@@ -80,7 +80,7 @@ module Polars
80
80
  super
81
81
  end
82
82
 
83
- # Get the struct definition as a name/dtype schema dict.
83
+ # Get the struct definition as a name/dtype schema hash.
84
84
  #
85
85
  # @return [Object]
86
86
  #
@@ -116,5 +116,23 @@ module Polars
116
116
  def unnest
117
117
  Utils.wrap_df(_s.struct_unnest)
118
118
  end
119
+
120
+ # Convert this struct to a string column with json values.
121
+ #
122
+ # @return [Series]
123
+ #
124
+ # @example
125
+ # s = Polars::Series.new("a", [{"a" => [1, 2], "b" => [45]}, {"a" => [9, 1, 3], "b" => nil}])
126
+ # s.struct.json_encode
127
+ # # =>
128
+ # # shape: (2,)
129
+ # # Series: 'a' [str]
130
+ # # [
131
+ # # "{"a":[1,2],"b":[45]}"
132
+ # # "{"a":[9,1,3],"b":null}"
133
+ # # ]
134
+ def json_encode
135
+ super
136
+ end
119
137
  end
120
138
  end
@@ -16,7 +16,10 @@ module Polars
16
16
  val.is_a?(::Array) && val.all? { |x| pathlike?(x) }
17
17
  end
18
18
 
19
- def self.is_bool_sequence(val)
19
+ def self.is_bool_sequence(val, include_series: false)
20
+ if include_series && val.is_a?(Series)
21
+ return val.dtype == Boolean
22
+ end
20
23
  val.is_a?(::Array) && val.all? { |x| x == true || x == false }
21
24
  end
22
25
 
@@ -24,6 +27,10 @@ module Polars
24
27
  val.is_a?(::Array) && _is_iterable_of(val, Integer)
25
28
  end
26
29
 
30
+ def self.is_sequence(val, include_series: false)
31
+ val.is_a?(::Array) || (include_series && val.is_a?(Series))
32
+ end
33
+
27
34
  def self.is_str_sequence(val, allow_str: false)
28
35
  if allow_str == false && val.is_a?(::String)
29
36
  false
@@ -76,5 +83,15 @@ module Polars
76
83
  end
77
84
  values
78
85
  end
86
+
87
+ def self.require_same_type(current, other)
88
+ if !other.is_a?(current.class) && !current.is_a?(other.class)
89
+ msg = (
90
+ "expected `other` to be a #{current.inspect}, " +
91
+ "not #{other.inspect}"
92
+ )
93
+ raise TypeError, msg
94
+ end
95
+ end
79
96
  end
80
97
  end
data/lib/polars/utils.rb CHANGED
@@ -163,7 +163,11 @@ module Polars
163
163
  end
164
164
 
165
165
  def self.parse_into_datatype_expr(input)
166
- raise Todo
166
+ if input.is_a?(DataTypeExpr)
167
+ input
168
+ else
169
+ parse_into_dtype(input).to_dtype_expr
170
+ end
167
171
  end
168
172
  end
169
173
  end
@@ -1,4 +1,4 @@
1
1
  module Polars
2
2
  # @private
3
- VERSION = "0.21.0"
3
+ VERSION = "0.21.1"
4
4
  end
data/lib/polars.rb CHANGED
@@ -29,6 +29,7 @@ require_relative "polars/convert"
29
29
  require_relative "polars/plot"
30
30
  require_relative "polars/data_frame"
31
31
  require_relative "polars/data_types"
32
+ require_relative "polars/data_type_expr"
32
33
  require_relative "polars/data_type_group"
33
34
  require_relative "polars/date_time_expr"
34
35
  require_relative "polars/date_time_name_space"
@@ -37,6 +38,7 @@ require_relative "polars/exceptions"
37
38
  require_relative "polars/expr"
38
39
  require_relative "polars/functions/as_datatype"
39
40
  require_relative "polars/functions/col"
41
+ require_relative "polars/functions/datatype"
40
42
  require_relative "polars/functions/eager"
41
43
  require_relative "polars/functions/lazy"
42
44
  require_relative "polars/functions/len"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polars-df
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.21.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -69,6 +69,7 @@ files:
69
69
  - ext/polars/src/exceptions.rs
70
70
  - ext/polars/src/expr/array.rs
71
71
  - ext/polars/src/expr/binary.rs
72
+ - ext/polars/src/expr/bitwise.rs
72
73
  - ext/polars/src/expr/categorical.rs
73
74
  - ext/polars/src/expr/datatype.rs
74
75
  - ext/polars/src/expr/datetime.rs
@@ -145,6 +146,7 @@ files:
145
146
  - lib/polars/config.rb
146
147
  - lib/polars/convert.rb
147
148
  - lib/polars/data_frame.rb
149
+ - lib/polars/data_type_expr.rb
148
150
  - lib/polars/data_type_group.rb
149
151
  - lib/polars/data_types.rb
150
152
  - lib/polars/date_time_expr.rb
@@ -157,6 +159,7 @@ files:
157
159
  - lib/polars/functions/aggregation/vertical.rb
158
160
  - lib/polars/functions/as_datatype.rb
159
161
  - lib/polars/functions/col.rb
162
+ - lib/polars/functions/datatype.rb
160
163
  - lib/polars/functions/eager.rb
161
164
  - lib/polars/functions/lazy.rb
162
165
  - lib/polars/functions/len.rb