polars-df 0.4.0-x86_64-darwin → 0.5.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,5 +60,37 @@ module Polars
60
60
  def rename_fields(names)
61
61
  super
62
62
  end
63
+
64
+ # Get the struct definition as a name/dtype schema dict.
65
+ #
66
+ # @return [Object]
67
+ def schema
68
+ if _s.nil?
69
+ {}
70
+ else
71
+ _s.dtype.to_schema
72
+ end
73
+ end
74
+
75
+ # Convert this struct Series to a DataFrame with a separate column for each field.
76
+ #
77
+ # @return [DataFrame]
78
+ #
79
+ # @example
80
+ # s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
81
+ # s.struct.unnest
82
+ # # =>
83
+ # # shape: (2, 2)
84
+ # # ┌─────┬─────┐
85
+ # # │ a ┆ b │
86
+ # # │ --- ┆ --- │
87
+ # # │ i64 ┆ i64 │
88
+ # # ╞═════╪═════╡
89
+ # # │ 1 ┆ 2 │
90
+ # # │ 3 ┆ 4 │
91
+ # # └─────┴─────┘
92
+ def unnest
93
+ Utils.wrap_df(_s.struct_unnest)
94
+ end
63
95
  end
64
96
  end
data/lib/polars/utils.rb CHANGED
@@ -23,24 +23,42 @@ module Polars
23
23
  Polars.col(name)
24
24
  end
25
25
 
26
+ def self.arrlen(obj)
27
+ if obj.is_a?(Range)
28
+ # size only works for numeric ranges
29
+ obj.to_a.length
30
+ elsif obj.is_a?(String)
31
+ nil
32
+ else
33
+ obj.length
34
+ end
35
+ rescue
36
+ nil
37
+ end
38
+
26
39
  def self._timedelta_to_pl_duration(td)
27
40
  td
28
41
  end
29
42
 
30
43
  def self._datetime_to_pl_timestamp(dt, tu)
31
44
  if tu == "ns"
32
- (dt.to_datetime.utc.to_f * 1e9).to_i
45
+ (dt.to_datetime.to_time.to_f * 1e9).to_i
33
46
  elsif tu == "us"
34
- (dt.to_datetime.utc.to_f * 1e6).to_i
47
+ (dt.to_datetime.to_time.to_f * 1e6).to_i
35
48
  elsif tu == "ms"
36
- (dt.to_datetime.utc.to_f * 1e3).to_i
49
+ (dt.to_datetime.to_time.to_f * 1e3).to_i
37
50
  elsif tu.nil?
38
- (dt.to_datetime.utc.to_f * 1e6).to_i
51
+ (dt.to_datetime.to_time.to_f * 1e6).to_i
39
52
  else
40
53
  raise ArgumentError, "tu must be one of {{'ns', 'us', 'ms'}}, got #{tu}"
41
54
  end
42
55
  end
43
56
 
57
+ def self._date_to_pl_date(d)
58
+ dt = d.to_datetime.to_time
59
+ dt.to_i / (3600 * 24)
60
+ end
61
+
44
62
  def self._to_ruby_datetime(value, dtype, tu: "ns", tz: nil)
45
63
  if dtype == :date || dtype == Date
46
64
  # days to seconds
@@ -69,6 +87,18 @@ module Polars
69
87
  end
70
88
  end
71
89
 
90
+ def self._to_ruby_duration(value, tu = "ns")
91
+ if tu == "ns"
92
+ value / 1e9
93
+ elsif tu == "us"
94
+ value / 1e6
95
+ elsif tu == "ms"
96
+ value / 1e3
97
+ else
98
+ raise ArgumentError, "tu must be one of {{'ns', 'us', 'ms'}}, got #{tu}"
99
+ end
100
+ end
101
+
72
102
  def self.selection_to_rbexpr_list(exprs)
73
103
  if exprs.is_a?(String) || exprs.is_a?(Symbol) || exprs.is_a?(Expr) || exprs.is_a?(Series)
74
104
  exprs = [exprs]
@@ -102,7 +132,10 @@ module Polars
102
132
  end
103
133
 
104
134
  # TODO fix
105
- def self.is_polars_dtype(data_type)
135
+ def self.is_polars_dtype(data_type, include_unknown: false)
136
+ if data_type == Unknown
137
+ return include_unknown
138
+ end
106
139
  data_type.is_a?(Symbol) || data_type.is_a?(String) || data_type.is_a?(DataType) || (data_type.is_a?(Class) && data_type < DataType)
107
140
  end
108
141
 
@@ -113,7 +146,8 @@ module Polars
113
146
  TrueClass => :bool,
114
147
  FalseClass => :bool,
115
148
  ::Date => :date,
116
- ::DateTime => :datetime
149
+ ::DateTime => :datetime,
150
+ ::Time => :datetime
117
151
  }
118
152
 
119
153
  # TODO fix
@@ -178,7 +212,7 @@ module Polars
178
212
  end
179
213
 
180
214
  def self.bool?(value)
181
- value == true || value == false
215
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
182
216
  end
183
217
 
184
218
  def self.strlike?(value)
@@ -1,4 +1,4 @@
1
1
  module Polars
2
2
  # @private
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
data/lib/polars.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # ext
2
2
  begin
3
- require_relative "polars/#{RUBY_VERSION.to_f}/polars"
3
+ require "polars/#{RUBY_VERSION.to_f}/polars"
4
4
  rescue LoadError
5
- require_relative "polars/polars"
5
+ require "polars/polars"
6
6
  end
7
7
 
8
8
  # stdlib
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.4.0
4
+ version: 0.5.0
5
5
  platform: x86_64-darwin
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-01 00:00:00.000000000 Z
11
+ date: 2023-05-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: andrew@ankane.org