polars-df 0.5.0-x86_64-darwin → 0.7.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
1
  module Polars
2
- # Series.arr namespace.
2
+ # Series.list namespace.
3
3
  class ListNameSpace
4
4
  include ExprDispatch
5
5
 
6
- self._accessor = "arr"
6
+ self._accessor = "list"
7
7
 
8
8
  # @private
9
9
  def initialize(series)
@@ -16,7 +16,7 @@ module Polars
16
16
  #
17
17
  # @example
18
18
  # s = Polars::Series.new([[1, 2, 3], [5]])
19
- # s.arr.lengths
19
+ # s.list.lengths
20
20
  # # =>
21
21
  # # shape: (2,)
22
22
  # # Series: '' [u32]
@@ -119,13 +119,13 @@ module Polars
119
119
  #
120
120
  # @example
121
121
  # s = Polars::Series.new([["foo", "bar"], ["hello", "world"]])
122
- # s.arr.join("-")
122
+ # s.list.join("-")
123
123
  # # =>
124
124
  # # shape: (2,)
125
125
  # # Series: '' [str]
126
126
  # # [
127
- # # "foo-bar"
128
- # # "hello-world"
127
+ # # "foo-bar"
128
+ # # "hello-world"
129
129
  # # ]
130
130
  def join(separator)
131
131
  super
@@ -180,7 +180,7 @@ module Polars
180
180
  #
181
181
  # @example
182
182
  # s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
183
- # s.arr.diff
183
+ # s.list.diff
184
184
  # # =>
185
185
  # # shape: (2,)
186
186
  # # Series: 'a' [list[i64]]
@@ -201,7 +201,7 @@ module Polars
201
201
  #
202
202
  # @example
203
203
  # s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
204
- # s.arr.shift
204
+ # s.list.shift
205
205
  # # =>
206
206
  # # shape: (2,)
207
207
  # # Series: 'a' [list[i64]]
@@ -225,7 +225,7 @@ module Polars
225
225
  #
226
226
  # @example
227
227
  # s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
228
- # s.arr.slice(1, 2)
228
+ # s.list.slice(1, 2)
229
229
  # # =>
230
230
  # # shape: (2,)
231
231
  # # Series: 'a' [list[i64]]
@@ -246,7 +246,7 @@ module Polars
246
246
  #
247
247
  # @example
248
248
  # s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
249
- # s.arr.head(2)
249
+ # s.list.head(2)
250
250
  # # =>
251
251
  # # shape: (2,)
252
252
  # # Series: 'a' [list[i64]]
@@ -267,7 +267,7 @@ module Polars
267
267
  #
268
268
  # @example
269
269
  # s = Polars::Series.new("a", [[1, 2, 3, 4], [10, 2, 1]])
270
- # s.arr.tail(2)
270
+ # s.list.tail(2)
271
271
  # # =>
272
272
  # # shape: (2,)
273
273
  # # Series: 'a' [list[i64]]
@@ -291,7 +291,7 @@ module Polars
291
291
  #
292
292
  # @example
293
293
  # df = Polars::DataFrame.new({"a" => [[1, 2, 3], [1, 2]]})
294
- # df.select([Polars.col("a").arr.to_struct])
294
+ # df.select([Polars.col("a").list.to_struct])
295
295
  # # =>
296
296
  # # shape: (2, 1)
297
297
  # # ┌────────────┐
@@ -315,7 +315,7 @@ module Polars
315
315
  # Run all expression parallel. Don't activate this blindly.
316
316
  # Parallelism is worth it if there is enough work to do per thread.
317
317
  #
318
- # This likely should not be use in the groupby context, because we already
318
+ # This likely should not be use in the group by context, because we already
319
319
  # parallel execution per group
320
320
  #
321
321
  # @return [Series]
@@ -323,14 +323,14 @@ module Polars
323
323
  # @example
324
324
  # df = Polars::DataFrame.new({"a" => [1, 8, 3], "b" => [4, 5, 2]})
325
325
  # df.with_column(
326
- # Polars.concat_list(["a", "b"]).arr.eval(Polars.element.rank).alias("rank")
326
+ # Polars.concat_list(["a", "b"]).list.eval(Polars.element.rank).alias("rank")
327
327
  # )
328
328
  # # =>
329
329
  # # shape: (3, 3)
330
330
  # # ┌─────┬─────┬────────────┐
331
331
  # # │ a ┆ b ┆ rank │
332
332
  # # │ --- ┆ --- ┆ --- │
333
- # # │ i64 ┆ i64 ┆ list[f32] │
333
+ # # │ i64 ┆ i64 ┆ list[f64] │
334
334
  # # ╞═════╪═════╪════════════╡
335
335
  # # │ 1 ┆ 4 ┆ [1.0, 2.0] │
336
336
  # # │ 8 ┆ 5 ┆ [2.0, 1.0] │
@@ -0,0 +1,198 @@
1
+ module Polars
2
+ # Namespace for expressions that operate on expression names.
3
+ class NameExpr
4
+ # @private
5
+ attr_accessor :_rbexpr
6
+
7
+ # @private
8
+ def initialize(expr)
9
+ self._rbexpr = expr._rbexpr
10
+ end
11
+
12
+ # Keep the original root name of the expression.
13
+ #
14
+ # @note
15
+ # Due to implementation constraints, this method can only be called as the last
16
+ # expression in a chain.
17
+ #
18
+ # @return [Expr]
19
+ #
20
+ # @example Prevent errors due to potential duplicate column names.
21
+ # df = Polars::DataFrame.new(
22
+ # {
23
+ # "a" => [1, 2],
24
+ # "b" => [3, 4]
25
+ # }
26
+ # )
27
+ # df.select((Polars.lit(10) / Polars.all).name.keep)
28
+ # # =>
29
+ # # shape: (2, 2)
30
+ # # ┌──────┬──────────┐
31
+ # # │ a ┆ b │
32
+ # # │ --- ┆ --- │
33
+ # # │ f64 ┆ f64 │
34
+ # # ╞══════╪══════════╡
35
+ # # │ 10.0 ┆ 3.333333 │
36
+ # # │ 5.0 ┆ 2.5 │
37
+ # # └──────┴──────────┘
38
+ #
39
+ # @example Undo an alias operation.
40
+ # df.with_columns((Polars.col("a") * 9).alias("c").name.keep)
41
+ # # =>
42
+ # # shape: (2, 2)
43
+ # # ┌─────┬─────┐
44
+ # # │ a ┆ b │
45
+ # # │ --- ┆ --- │
46
+ # # │ i64 ┆ i64 │
47
+ # # ╞═════╪═════╡
48
+ # # │ 9 ┆ 3 │
49
+ # # │ 18 ┆ 4 │
50
+ # # └─────┴─────┘
51
+ def keep
52
+ Utils.wrap_expr(_rbexpr.name_keep)
53
+ end
54
+
55
+ # Rename the output of an expression by mapping a function over the root name.
56
+ #
57
+ # @return [Expr]
58
+ #
59
+ # @example Remove a common suffix and convert to lower case.
60
+ # df = Polars::DataFrame.new(
61
+ # {
62
+ # "A_reverse" => [3, 2, 1],
63
+ # "B_reverse" => ["z", "y", "x"]
64
+ # }
65
+ # )
66
+ # df.with_columns(
67
+ # Polars.all.reverse.name.map { |c| c.delete_suffix("_reverse").downcase }
68
+ # )
69
+ # # =>
70
+ # # shape: (3, 4)
71
+ # # ┌───────────┬───────────┬─────┬─────┐
72
+ # # │ A_reverse ┆ B_reverse ┆ a ┆ b │
73
+ # # │ --- ┆ --- ┆ --- ┆ --- │
74
+ # # │ i64 ┆ str ┆ i64 ┆ str │
75
+ # # ╞═══════════╪═══════════╪═════╪═════╡
76
+ # # │ 3 ┆ z ┆ 1 ┆ x │
77
+ # # │ 2 ┆ y ┆ 2 ┆ y │
78
+ # # │ 1 ┆ x ┆ 3 ┆ z │
79
+ # # └───────────┴───────────┴─────┴─────┘
80
+ def map(&f)
81
+ Utils.wrap_expr(_rbexpr.name_map(f))
82
+ end
83
+
84
+ # Add a prefix to the root column name of the expression.
85
+ #
86
+ # @param prefix [Object]
87
+ # Prefix to add to the root column name.
88
+ #
89
+ # @return [Expr]
90
+ #
91
+ # @example
92
+ # df = Polars::DataFrame.new(
93
+ # {
94
+ # "a" => [1, 2, 3],
95
+ # "b" => ["x", "y", "z"]
96
+ # }
97
+ # )
98
+ # df.with_columns(Polars.all.reverse.name.prefix("reverse_"))
99
+ # # =>
100
+ # # shape: (3, 4)
101
+ # # ┌─────┬─────┬───────────┬───────────┐
102
+ # # │ a ┆ b ┆ reverse_a ┆ reverse_b │
103
+ # # │ --- ┆ --- ┆ --- ┆ --- │
104
+ # # │ i64 ┆ str ┆ i64 ┆ str │
105
+ # # ╞═════╪═════╪═══════════╪═══════════╡
106
+ # # │ 1 ┆ x ┆ 3 ┆ z │
107
+ # # │ 2 ┆ y ┆ 2 ┆ y │
108
+ # # │ 3 ┆ z ┆ 1 ┆ x │
109
+ # # └─────┴─────┴───────────┴───────────┘
110
+ def prefix(prefix)
111
+ Utils.wrap_expr(_rbexpr.name_prefix(prefix))
112
+ end
113
+
114
+ # Add a suffix to the root column name of the expression.
115
+ #
116
+ # @param suffix [Object]
117
+ # Suffix to add to the root column name.
118
+ #
119
+ # @return [Expr]
120
+ #
121
+ # @example
122
+ # df = Polars::DataFrame.new(
123
+ # {
124
+ # "a" => [1, 2, 3],
125
+ # "b" => ["x", "y", "z"]
126
+ # }
127
+ # )
128
+ # df.with_columns(Polars.all.reverse.name.suffix("_reverse"))
129
+ # # =>
130
+ # # shape: (3, 4)
131
+ # # ┌─────┬─────┬───────────┬───────────┐
132
+ # # │ a ┆ b ┆ a_reverse ┆ b_reverse │
133
+ # # │ --- ┆ --- ┆ --- ┆ --- │
134
+ # # │ i64 ┆ str ┆ i64 ┆ str │
135
+ # # ╞═════╪═════╪═══════════╪═══════════╡
136
+ # # │ 1 ┆ x ┆ 3 ┆ z │
137
+ # # │ 2 ┆ y ┆ 2 ┆ y │
138
+ # # │ 3 ┆ z ┆ 1 ┆ x │
139
+ # # └─────┴─────┴───────────┴───────────┘
140
+ def suffix(suffix)
141
+ Utils.wrap_expr(_rbexpr.name_suffix(suffix))
142
+ end
143
+
144
+ # Make the root column name lowercase.
145
+ #
146
+ # @return [Expr]
147
+ #
148
+ # @example
149
+ # df = Polars::DataFrame.new(
150
+ # {
151
+ # "ColX" => [1, 2, 3],
152
+ # "ColY" => ["x", "y", "z"],
153
+ # }
154
+ # )
155
+ # df.with_columns(Polars.all.name.to_lowercase)
156
+ # # =>
157
+ # # shape: (3, 4)
158
+ # # ┌──────┬──────┬──────┬──────┐
159
+ # # │ ColX ┆ ColY ┆ colx ┆ coly │
160
+ # # │ --- ┆ --- ┆ --- ┆ --- │
161
+ # # │ i64 ┆ str ┆ i64 ┆ str │
162
+ # # ╞══════╪══════╪══════╪══════╡
163
+ # # │ 1 ┆ x ┆ 1 ┆ x │
164
+ # # │ 2 ┆ y ┆ 2 ┆ y │
165
+ # # │ 3 ┆ z ┆ 3 ┆ z │
166
+ # # └──────┴──────┴──────┴──────┘
167
+ def to_lowercase
168
+ Utils.wrap_expr(_rbexpr.name_to_lowercase)
169
+ end
170
+
171
+ # Make the root column name uppercase.
172
+ #
173
+ # @return [Expr]
174
+ #
175
+ # @example
176
+ # df = Polars::DataFrame.new(
177
+ # {
178
+ # "ColX" => [1, 2, 3],
179
+ # "ColY" => ["x", "y", "z"]
180
+ # }
181
+ # )
182
+ # df.with_columns(Polars.all.name.to_uppercase)
183
+ # # =>
184
+ # # shape: (3, 4)
185
+ # # ┌──────┬──────┬──────┬──────┐
186
+ # # │ ColX ┆ ColY ┆ COLX ┆ COLY │
187
+ # # │ --- ┆ --- ┆ --- ┆ --- │
188
+ # # │ i64 ┆ str ┆ i64 ┆ str │
189
+ # # ╞══════╪══════╪══════╪══════╡
190
+ # # │ 1 ┆ x ┆ 1 ┆ x │
191
+ # # │ 2 ┆ y ┆ 2 ┆ y │
192
+ # # │ 3 ┆ z ┆ 3 ┆ z │
193
+ # # └──────┴──────┴──────┴──────┘
194
+ def to_uppercase
195
+ Utils.wrap_expr(_rbexpr.name_to_uppercase)
196
+ end
197
+ end
198
+ end
@@ -2,7 +2,7 @@ module Polars
2
2
  # A rolling grouper.
3
3
  #
4
4
  # This has an `.agg` method which will allow you to run all polars expressions in a
5
- # groupby context.
5
+ # group by context.
6
6
  class RollingGroupBy
7
7
  def initialize(
8
8
  df,
@@ -10,7 +10,8 @@ module Polars
10
10
  period,
11
11
  offset,
12
12
  closed,
13
- by
13
+ by,
14
+ check_sorted
14
15
  )
15
16
  period = Utils._timedelta_to_pl_duration(period)
16
17
  offset = Utils._timedelta_to_pl_duration(offset)
@@ -21,12 +22,13 @@ module Polars
21
22
  @offset = offset
22
23
  @closed = closed
23
24
  @by = by
25
+ @check_sorted = check_sorted
24
26
  end
25
27
 
26
28
  def agg(aggs)
27
29
  @df.lazy
28
- .groupby_rolling(
29
- index_column: @time_column, period: @period, offset: @offset, closed: @closed, by: @by
30
+ .group_by_rolling(
31
+ index_column: @time_column, period: @period, offset: @offset, closed: @closed, by: @by, check_sorted: @check_sorted
30
32
  )
31
33
  .agg(aggs)
32
34
  .collect(no_optimization: true, string_cache: false)
data/lib/polars/series.rb CHANGED
@@ -65,7 +65,7 @@ module Polars
65
65
  )
66
66
  .rename(name, in_place: true)
67
67
  ._s
68
- elsif values.is_a?(Array)
68
+ elsif values.is_a?(::Array)
69
69
  self._s = sequence_to_rbseries(name, values, dtype: dtype, strict: strict, dtype_if_empty: dtype_if_empty)
70
70
  elsif defined?(Numo::NArray) && values.is_a?(Numo::NArray)
71
71
  self._s = numo_to_rbseries(name, values, strict: strict, nan_to_null: nan_to_null)
@@ -317,6 +317,10 @@ module Polars
317
317
  end
318
318
 
319
319
  if item.is_a?(Integer)
320
+ if item < 0
321
+ item = len + item
322
+ end
323
+
320
324
  return _s.get_idx(item)
321
325
  end
322
326
 
@@ -335,7 +339,7 @@ module Polars
335
339
  #
336
340
  # @return [Object]
337
341
  def []=(key, value)
338
- if value.is_a?(Array)
342
+ if value.is_a?(::Array)
339
343
  if is_numeric || is_datelike
340
344
  set_at_idx(key, value)
341
345
  return
@@ -353,7 +357,7 @@ module Polars
353
357
  else
354
358
  raise Todo
355
359
  end
356
- elsif key.is_a?(Array)
360
+ elsif key.is_a?(::Array)
357
361
  s = Utils.wrap_s(sequence_to_rbseries("", key, dtype: UInt32))
358
362
  self[s] = value
359
363
  elsif key.is_a?(Range)
@@ -428,6 +432,18 @@ module Polars
428
432
  end
429
433
  alias_method :all, :all?
430
434
 
435
+ # Check if all boolean values in the column are `false`.
436
+ #
437
+ # @return [Boolean]
438
+ def none?(&block)
439
+ if block_given?
440
+ apply(&block).none?
441
+ else
442
+ to_frame.select(Polars.col(name).is_not.all).to_series[0]
443
+ end
444
+ end
445
+ alias_method :none, :none?
446
+
431
447
  # Compute the logarithm to a given base.
432
448
  #
433
449
  # @param base [Float]
@@ -715,8 +731,8 @@ module Polars
715
731
  # # │ 0 ┆ 1 ┆ 0 │
716
732
  # # │ 0 ┆ 0 ┆ 1 │
717
733
  # # └─────┴─────┴─────┘
718
- def to_dummies(separator: "_")
719
- Utils.wrap_df(_s.to_dummies(separator))
734
+ def to_dummies(separator: "_", drop_first: false)
735
+ Utils.wrap_df(_s.to_dummies(separator, drop_first))
720
736
  end
721
737
 
722
738
  # Count the unique values in a Series.
@@ -795,7 +811,7 @@ module Polars
795
811
  # Number of valid values there should be in the window before the expression
796
812
  # is evaluated. valid values = `length - null_count`
797
813
  # @param parallel [Boolean]
798
- # Run in parallel. Don't do this in a groupby or another operation that
814
+ # Run in parallel. Don't do this in a group by or another operation that
799
815
  # already has much parallelization.
800
816
  #
801
817
  # @return [Series]
@@ -1124,7 +1140,7 @@ module Polars
1124
1140
  # # 3
1125
1141
  # # ]
1126
1142
  def filter(predicate)
1127
- if predicate.is_a?(Array)
1143
+ if predicate.is_a?(::Array)
1128
1144
  predicate = Series.new("", predicate)
1129
1145
  end
1130
1146
  Utils.wrap_s(_s.filter(predicate._s))
@@ -2813,7 +2829,8 @@ module Polars
2813
2829
  window_size,
2814
2830
  weights: nil,
2815
2831
  min_periods: nil,
2816
- center: false
2832
+ center: false,
2833
+ ddof: 1
2817
2834
  )
2818
2835
  to_frame
2819
2836
  .select(
@@ -2821,7 +2838,8 @@ module Polars
2821
2838
  window_size,
2822
2839
  weights: weights,
2823
2840
  min_periods: min_periods,
2824
- center: center
2841
+ center: center,
2842
+ ddof: ddof
2825
2843
  )
2826
2844
  )
2827
2845
  .to_series
@@ -2864,7 +2882,8 @@ module Polars
2864
2882
  window_size,
2865
2883
  weights: nil,
2866
2884
  min_periods: nil,
2867
- center: false
2885
+ center: false,
2886
+ ddof: 1
2868
2887
  )
2869
2888
  to_frame
2870
2889
  .select(
@@ -2872,7 +2891,8 @@ module Polars
2872
2891
  window_size,
2873
2892
  weights: weights,
2874
2893
  min_periods: min_periods,
2875
- center: center
2894
+ center: center,
2895
+ ddof: ddof
2876
2896
  )
2877
2897
  )
2878
2898
  .to_series
@@ -3089,7 +3109,7 @@ module Polars
3089
3109
  # s.peak_max
3090
3110
  # # =>
3091
3111
  # # shape: (5,)
3092
- # # Series: '' [bool]
3112
+ # # Series: 'a' [bool]
3093
3113
  # # [
3094
3114
  # # false
3095
3115
  # # false
@@ -3098,7 +3118,7 @@ module Polars
3098
3118
  # # true
3099
3119
  # # ]
3100
3120
  def peak_max
3101
- Utils.wrap_s(_s.peak_max)
3121
+ super
3102
3122
  end
3103
3123
 
3104
3124
  # Get a boolean mask of the local minimum peaks.
@@ -3110,7 +3130,7 @@ module Polars
3110
3130
  # s.peak_min
3111
3131
  # # =>
3112
3132
  # # shape: (5,)
3113
- # # Series: '' [bool]
3133
+ # # Series: 'a' [bool]
3114
3134
  # # [
3115
3135
  # # false
3116
3136
  # # true
@@ -3119,7 +3139,7 @@ module Polars
3119
3139
  # # false
3120
3140
  # # ]
3121
3141
  def peak_min
3122
- Utils.wrap_s(_s.peak_min)
3142
+ super
3123
3143
  end
3124
3144
 
3125
3145
  # Count the number of unique values in this Series.
@@ -3203,13 +3223,13 @@ module Polars
3203
3223
  # s.interpolate
3204
3224
  # # =>
3205
3225
  # # shape: (5,)
3206
- # # Series: 'a' [i64]
3226
+ # # Series: 'a' [f64]
3207
3227
  # # [
3208
- # # 1
3209
- # # 2
3210
- # # 3
3211
- # # 4
3212
- # # 5
3228
+ # # 1.0
3229
+ # # 2.0
3230
+ # # 3.0
3231
+ # # 4.0
3232
+ # # 5.0
3213
3233
  # # ]
3214
3234
  def interpolate(method: "linear")
3215
3235
  super
@@ -3252,7 +3272,7 @@ module Polars
3252
3272
  # s.rank
3253
3273
  # # =>
3254
3274
  # # shape: (5,)
3255
- # # Series: 'a' [f32]
3275
+ # # Series: 'a' [f64]
3256
3276
  # # [
3257
3277
  # # 3.0
3258
3278
  # # 4.5
@@ -3581,10 +3601,17 @@ module Polars
3581
3601
  # Create an object namespace of all list related methods.
3582
3602
  #
3583
3603
  # @return [ListNameSpace]
3584
- def arr
3604
+ def list
3585
3605
  ListNameSpace.new(self)
3586
3606
  end
3587
3607
 
3608
+ # Create an object namespace of all array related methods.
3609
+ #
3610
+ # @return [ArrayNameSpace]
3611
+ def arr
3612
+ ArrayNameSpace.new(self)
3613
+ end
3614
+
3588
3615
  # Create an object namespace of all binary related methods.
3589
3616
  #
3590
3617
  # @return [BinaryNameSpace]
@@ -3824,9 +3851,12 @@ module Polars
3824
3851
 
3825
3852
  if (values.nil? || values.empty?) && dtype.nil?
3826
3853
  dtype = dtype_if_empty || Float32
3854
+ elsif dtype == List
3855
+ ruby_dtype = ::Array
3827
3856
  end
3828
3857
 
3829
3858
  rb_temporal_types = [::Date, ::DateTime, ::Time]
3859
+ rb_temporal_types << ActiveSupport::TimeWithZone if defined?(ActiveSupport::TimeWithZone)
3830
3860
 
3831
3861
  value = _get_first_non_none(values)
3832
3862
  if !value.nil?
@@ -3835,9 +3865,20 @@ module Polars
3835
3865
  end
3836
3866
  end
3837
3867
 
3838
- if !dtype.nil? && Utils.is_polars_dtype(dtype) && ruby_dtype.nil?
3868
+ if !dtype.nil? && ![List, Unknown].include?(dtype) && Utils.is_polars_dtype(dtype) && ruby_dtype.nil?
3869
+ if dtype == Array && !dtype.is_a?(Array) && value.is_a?(::Array)
3870
+ dtype = Array.new(value.size)
3871
+ end
3872
+
3839
3873
  constructor = polars_type_to_constructor(dtype)
3840
3874
  rbseries = constructor.call(name, values, strict)
3875
+
3876
+ base_type = dtype.is_a?(DataType) ? dtype.class : dtype
3877
+ if [Date, Datetime, Duration, Time, Categorical, Boolean].include?(base_type)
3878
+ if rbseries.dtype != dtype
3879
+ rbseries = rbseries.cast(dtype, true)
3880
+ end
3881
+ end
3841
3882
  return rbseries
3842
3883
  else
3843
3884
  if ruby_dtype.nil?
@@ -3868,7 +3909,17 @@ module Polars
3868
3909
  return s._s
3869
3910
  elsif defined?(Numo::NArray) && value.is_a?(Numo::NArray) && value.shape.length == 1
3870
3911
  raise Todo
3871
- elsif ruby_dtype == Array
3912
+ elsif ruby_dtype == ::Array
3913
+ if dtype.is_a?(Object)
3914
+ return RbSeries.new_object(name, values, strict)
3915
+ end
3916
+ if dtype
3917
+ srs = sequence_from_anyvalue_or_object(name, values)
3918
+ if dtype != srs.dtype
3919
+ srs = srs.cast(dtype, strict: false)
3920
+ end
3921
+ return srs
3922
+ end
3872
3923
  return sequence_from_anyvalue_or_object(name, values)
3873
3924
  elsif ruby_dtype == Series
3874
3925
  return RbSeries.new_series_list(name, values.map(&:_s), strict)
@@ -3910,9 +3961,17 @@ module Polars
3910
3961
  UInt16 => RbSeries.method(:new_opt_u16),
3911
3962
  UInt32 => RbSeries.method(:new_opt_u32),
3912
3963
  UInt64 => RbSeries.method(:new_opt_u64),
3964
+ Decimal => RbSeries.method(:new_decimal),
3965
+ Date => RbSeries.method(:new_from_anyvalues),
3966
+ Datetime => RbSeries.method(:new_from_anyvalues),
3967
+ Duration => RbSeries.method(:new_from_anyvalues),
3968
+ Time => RbSeries.method(:new_from_anyvalues),
3913
3969
  Boolean => RbSeries.method(:new_opt_bool),
3914
3970
  Utf8 => RbSeries.method(:new_str),
3915
- Binary => RbSeries.method(:new_binary)
3971
+ Object => RbSeries.method(:new_object),
3972
+ Categorical => RbSeries.method(:new_str),
3973
+ Binary => RbSeries.method(:new_binary),
3974
+ Null => RbSeries.method(:new_null)
3916
3975
  }
3917
3976
 
3918
3977
  SYM_TYPE_TO_CONSTRUCTOR = {
@@ -3931,8 +3990,14 @@ module Polars
3931
3990
  }
3932
3991
 
3933
3992
  def polars_type_to_constructor(dtype)
3934
- if dtype.is_a?(Class) && dtype < DataType
3993
+ if dtype.is_a?(Array)
3994
+ lambda do |name, values, strict|
3995
+ RbSeries.new_array(dtype.width, dtype.inner, name, values, strict)
3996
+ end
3997
+ elsif dtype.is_a?(Class) && dtype < DataType
3935
3998
  POLARS_TYPE_TO_CONSTRUCTOR.fetch(dtype)
3999
+ elsif dtype.is_a?(DataType)
4000
+ POLARS_TYPE_TO_CONSTRUCTOR.fetch(dtype.class)
3936
4001
  else
3937
4002
  SYM_TYPE_TO_CONSTRUCTOR.fetch(dtype.to_sym)
3938
4003
  end
@@ -3944,7 +4009,9 @@ module Polars
3944
4009
  Float => RbSeries.method(:new_opt_f64),
3945
4010
  Integer => RbSeries.method(:new_opt_i64),
3946
4011
  TrueClass => RbSeries.method(:new_opt_bool),
3947
- FalseClass => RbSeries.method(:new_opt_bool)
4012
+ FalseClass => RbSeries.method(:new_opt_bool),
4013
+ BigDecimal => RbSeries.method(:new_decimal),
4014
+ NilClass => RbSeries.method(:new_null)
3948
4015
  }
3949
4016
 
3950
4017
  def rb_type_to_constructor(dtype)