polars-df 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,17 @@
1
1
  module Polars
2
+ # Namespace for struct related expressions.
2
3
  class StructExpr
4
+ # @private
3
5
  attr_accessor :_rbexpr
4
6
 
7
+ # @private
5
8
  def initialize(expr)
6
9
  self._rbexpr = expr._rbexpr
7
10
  end
8
11
 
12
+ # Retrieve one of the fields of this `Struct` as a new Series.
13
+ #
14
+ # @return [Expr]
9
15
  def [](item)
10
16
  if item.is_a?(String)
11
17
  field(item)
@@ -16,10 +22,77 @@ module Polars
16
22
  end
17
23
  end
18
24
 
25
+ # Retrieve one of the fields of this `Struct` as a new Series.
26
+ #
27
+ # @param name [String]
28
+ # Name of the field
29
+ #
30
+ # @return [Expr]
31
+ #
32
+ # @example
33
+ # df = (
34
+ # Polars::DataFrame.new(
35
+ # {
36
+ # "int" => [1, 2],
37
+ # "str" => ["a", "b"],
38
+ # "bool" => [true, nil],
39
+ # "list" => [[1, 2], [3]]
40
+ # }
41
+ # )
42
+ # .to_struct("my_struct")
43
+ # .to_frame
44
+ # )
45
+ # df.select(Polars.col("my_struct").struct.field("str"))
46
+ # # =>
47
+ # # shape: (2, 1)
48
+ # # ┌─────┐
49
+ # # │ str │
50
+ # # │ --- │
51
+ # # │ str │
52
+ # # ╞═════╡
53
+ # # │ a │
54
+ # # ├╌╌╌╌╌┤
55
+ # # │ b │
56
+ # # └─────┘
19
57
  def field(name)
20
58
  Utils.wrap_expr(_rbexpr.struct_field_by_name(name))
21
59
  end
22
60
 
61
+ # Rename the fields of the struct.
62
+ #
63
+ # @param names [Array]
64
+ # New names in the order of the struct's fields
65
+ #
66
+ # @return [Expr]
67
+ #
68
+ # @example
69
+ # df = (
70
+ # Polars::DataFrame.new(
71
+ # {
72
+ # "int" => [1, 2],
73
+ # "str" => ["a", "b"],
74
+ # "bool" => [true, nil],
75
+ # "list" => [[1, 2], [3]]
76
+ # }
77
+ # )
78
+ # .to_struct("my_struct")
79
+ # .to_frame
80
+ # )
81
+ # df = df.with_column(
82
+ # Polars.col("my_struct").struct.rename_fields(["INT", "STR", "BOOL", "LIST"])
83
+ # )
84
+ # df.select(Polars.col("my_struct").struct.field("INT"))
85
+ # # =>
86
+ # # shape: (2, 1)
87
+ # # ┌─────┐
88
+ # # │ INT │
89
+ # # │ --- │
90
+ # # │ i64 │
91
+ # # ╞═════╡
92
+ # # │ 1 │
93
+ # # ├╌╌╌╌╌┤
94
+ # # │ 2 │
95
+ # # └─────┘
23
96
  def rename_fields(names)
24
97
  Utils.wrap_expr(_rbexpr.struct_rename_fields(names))
25
98
  end
data/lib/polars/utils.rb CHANGED
@@ -19,6 +19,24 @@ module Polars
19
19
  Polars.col(name)
20
20
  end
21
21
 
22
+ def self._timedelta_to_pl_duration(td)
23
+ td
24
+ end
25
+
26
+ def self._datetime_to_pl_timestamp(dt, tu)
27
+ if tu == "ns"
28
+ (dt.to_datetime.utc.to_f * 1e9).to_i
29
+ elsif tu == "us"
30
+ (dt.to_datetime.utc.to_f * 1e6).to_i
31
+ elsif tu == "ms"
32
+ (dt.to_datetime.utc.to_f * 1e3).to_i
33
+ elsif tu.nil?
34
+ (dt.to_datetime.utc.to_f * 1e6).to_i
35
+ else
36
+ raise ArgumentError, "tu must be one of {{'ns', 'us', 'ms'}}, got #{tu}"
37
+ end
38
+ end
39
+
22
40
  def self.selection_to_rbexpr_list(exprs)
23
41
  if exprs.is_a?(String) || exprs.is_a?(Expr) || exprs.is_a?(Series)
24
42
  exprs = [exprs]
@@ -49,12 +67,30 @@ module Polars
49
67
 
50
68
  # TODO fix
51
69
  def self.is_polars_dtype(data_type)
52
- true
70
+ data_type.is_a?(Symbol) || data_type.is_a?(String)
53
71
  end
54
72
 
73
+ RB_TYPE_TO_DTYPE = {
74
+ Float => :f64,
75
+ Integer => :i64,
76
+ String => :str,
77
+ TrueClass => :bool,
78
+ FalseClass => :bool,
79
+ Date => :date,
80
+ DateTime => :datetime
81
+ }
82
+
55
83
  # TODO fix
56
- def self.rb_type_to_dtype(dtype)
57
- dtype.to_s
84
+ def self.rb_type_to_dtype(data_type)
85
+ if is_polars_dtype(data_type)
86
+ return data_type.to_s
87
+ end
88
+
89
+ begin
90
+ RB_TYPE_TO_DTYPE.fetch(data_type).to_s
91
+ rescue KeyError
92
+ raise ArgumentError, "Conversion of Ruby data type #{data_type} to Polars data type not implemented."
93
+ end
58
94
  end
59
95
 
60
96
  def self._process_null_values(null_values)
@@ -103,5 +139,9 @@ module Polars
103
139
  sz
104
140
  end
105
141
  end
142
+
143
+ def self.bool?(value)
144
+ value == true || value == false
145
+ end
106
146
  end
107
147
  end
@@ -1,3 +1,4 @@
1
1
  module Polars
2
- VERSION = "0.1.2"
2
+ # @private
3
+ VERSION = "0.1.3"
3
4
  end
data/lib/polars/when.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Polars
2
+ # @private
2
3
  class When
3
4
  attr_accessor :_rbwhen
4
5
 
@@ -1,4 +1,5 @@
1
1
  module Polars
2
+ # @private
2
3
  class WhenThen
3
4
  attr_accessor :_rbwhenthen
4
5
 
data/lib/polars.rb CHANGED
@@ -1,13 +1,19 @@
1
1
  # ext
2
2
  require "polars/polars"
3
3
 
4
+ # stdlib
5
+ require "date"
6
+
4
7
  # modules
8
+ require "polars/expr_dispatch"
5
9
  require "polars/batched_csv_reader"
6
10
  require "polars/cat_expr"
7
11
  require "polars/data_frame"
8
12
  require "polars/date_time_expr"
13
+ require "polars/exceptions"
9
14
  require "polars/expr"
10
15
  require "polars/functions"
16
+ require "polars/group_by"
11
17
  require "polars/io"
12
18
  require "polars/lazy_frame"
13
19
  require "polars/lazy_functions"
@@ -15,6 +21,7 @@ require "polars/lazy_group_by"
15
21
  require "polars/list_expr"
16
22
  require "polars/meta_expr"
17
23
  require "polars/series"
24
+ require "polars/slice"
18
25
  require "polars/string_expr"
19
26
  require "polars/struct_expr"
20
27
  require "polars/utils"
@@ -23,16 +30,6 @@ require "polars/when"
23
30
  require "polars/when_then"
24
31
 
25
32
  module Polars
26
- # @private
27
- class Error < StandardError; end
28
-
29
- # @private
30
- class Todo < Error
31
- def message
32
- "not implemented yet"
33
- end
34
- end
35
-
36
33
  extend Functions
37
34
  extend IO
38
35
  extend LazyFunctions
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polars-df
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -31,6 +31,7 @@ extensions:
31
31
  - ext/polars/extconf.rb
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ".yardopts"
34
35
  - CHANGELOG.md
35
36
  - Cargo.lock
36
37
  - Cargo.toml
@@ -51,14 +52,19 @@ files:
51
52
  - ext/polars/src/lazy/utils.rs
52
53
  - ext/polars/src/lib.rs
53
54
  - ext/polars/src/series.rs
55
+ - ext/polars/src/set.rs
56
+ - ext/polars/src/utils.rs
54
57
  - lib/polars-df.rb
55
58
  - lib/polars.rb
56
59
  - lib/polars/batched_csv_reader.rb
57
60
  - lib/polars/cat_expr.rb
58
61
  - lib/polars/data_frame.rb
59
62
  - lib/polars/date_time_expr.rb
63
+ - lib/polars/exceptions.rb
60
64
  - lib/polars/expr.rb
65
+ - lib/polars/expr_dispatch.rb
61
66
  - lib/polars/functions.rb
67
+ - lib/polars/group_by.rb
62
68
  - lib/polars/io.rb
63
69
  - lib/polars/lazy_frame.rb
64
70
  - lib/polars/lazy_functions.rb
@@ -66,6 +72,7 @@ files:
66
72
  - lib/polars/list_expr.rb
67
73
  - lib/polars/meta_expr.rb
68
74
  - lib/polars/series.rb
75
+ - lib/polars/slice.rb
69
76
  - lib/polars/string_expr.rb
70
77
  - lib/polars/struct_expr.rb
71
78
  - lib/polars/utils.rb