polars-df 0.23.0 → 0.24.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 +127 -1
- data/Cargo.lock +72 -58
- data/README.md +31 -27
- data/ext/polars/Cargo.toml +15 -6
- data/ext/polars/src/batched_csv.rs +35 -39
- 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/unity.rs +123 -101
- data/ext/polars/src/conversion/any_value.rs +13 -17
- data/ext/polars/src/conversion/chunked_array.rs +5 -5
- data/ext/polars/src/conversion/datetime.rs +3 -2
- data/ext/polars/src/conversion/mod.rs +50 -45
- data/ext/polars/src/dataframe/export.rs +13 -13
- data/ext/polars/src/dataframe/general.rs +223 -223
- data/ext/polars/src/dataframe/io.rs +27 -141
- data/ext/polars/src/dataframe/mod.rs +13 -5
- data/ext/polars/src/dataframe/serde.rs +1 -1
- data/ext/polars/src/error.rs +44 -7
- data/ext/polars/src/exceptions.rs +45 -12
- data/ext/polars/src/expr/array.rs +12 -0
- data/ext/polars/src/expr/datatype.rs +2 -2
- data/ext/polars/src/expr/datetime.rs +4 -5
- data/ext/polars/src/expr/general.rs +49 -13
- data/ext/polars/src/expr/list.rs +4 -0
- data/ext/polars/src/expr/meta.rs +8 -3
- data/ext/polars/src/expr/mod.rs +22 -6
- data/ext/polars/src/expr/name.rs +19 -8
- data/ext/polars/src/expr/rolling.rs +50 -1
- data/ext/polars/src/expr/string.rs +0 -1
- data/ext/polars/src/expr/struct.rs +7 -2
- data/ext/polars/src/file.rs +136 -103
- data/ext/polars/src/functions/aggregation.rs +9 -8
- data/ext/polars/src/functions/io.rs +81 -10
- data/ext/polars/src/functions/lazy.rs +95 -21
- data/ext/polars/src/functions/mod.rs +2 -0
- data/ext/polars/src/functions/range.rs +19 -3
- data/ext/polars/src/functions/strings.rs +6 -0
- data/ext/polars/src/functions/utils.rs +6 -0
- data/ext/polars/src/interop/arrow/mod.rs +50 -1
- data/ext/polars/src/interop/arrow/{to_ruby.rs → to_rb.rs} +30 -0
- 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 +1 -1
- data/ext/polars/src/lazyframe/exitable.rs +39 -0
- data/ext/polars/src/lazyframe/general.rs +340 -236
- data/ext/polars/src/lazyframe/mod.rs +46 -10
- data/ext/polars/src/lazyframe/optflags.rs +5 -4
- data/ext/polars/src/lazyframe/serde.rs +11 -3
- data/ext/polars/src/lazyframe/sink.rs +10 -5
- data/ext/polars/src/lazygroupby.rs +6 -7
- data/ext/polars/src/lib.rs +141 -76
- data/ext/polars/src/map/dataframe.rs +12 -12
- data/ext/polars/src/map/lazy.rs +7 -5
- data/ext/polars/src/map/mod.rs +15 -8
- data/ext/polars/src/map/series.rs +3 -3
- data/ext/polars/src/on_startup.rs +16 -8
- data/ext/polars/src/prelude.rs +1 -0
- data/ext/polars/src/rb_modules.rs +19 -49
- data/ext/polars/src/series/aggregation.rs +79 -140
- 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 +17 -18
- data/ext/polars/src/series/export.rs +1 -1
- data/ext/polars/src/series/general.rs +254 -289
- data/ext/polars/src/series/import.rs +17 -0
- data/ext/polars/src/series/map.rs +178 -160
- data/ext/polars/src/series/mod.rs +28 -12
- data/ext/polars/src/series/scatter.rs +12 -9
- 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 +159 -1
- data/lib/polars/array_expr.rb +81 -12
- data/lib/polars/array_name_space.rb +74 -7
- data/lib/polars/batched_csv_reader.rb +21 -21
- data/lib/polars/binary_name_space.rb +1 -1
- data/lib/polars/cat_expr.rb +7 -7
- data/lib/polars/config.rb +1 -1
- data/lib/polars/convert.rb +189 -34
- data/lib/polars/data_frame.rb +1066 -831
- data/lib/polars/data_frame_plot.rb +173 -0
- data/lib/polars/data_type_group.rb +1 -0
- data/lib/polars/data_types.rb +31 -12
- data/lib/polars/date_time_expr.rb +51 -69
- data/lib/polars/date_time_name_space.rb +80 -112
- data/lib/polars/dynamic_group_by.rb +7 -7
- data/lib/polars/exceptions.rb +50 -10
- data/lib/polars/expr.rb +470 -517
- data/lib/polars/functions/aggregation/horizontal.rb +0 -1
- data/lib/polars/functions/aggregation/vertical.rb +2 -3
- data/lib/polars/functions/as_datatype.rb +290 -8
- data/lib/polars/functions/eager.rb +204 -10
- data/lib/polars/functions/escape_regex.rb +21 -0
- data/lib/polars/functions/lazy.rb +409 -169
- data/lib/polars/functions/lit.rb +17 -1
- data/lib/polars/functions/range/int_range.rb +74 -2
- data/lib/polars/functions/range/linear_space.rb +77 -0
- data/lib/polars/functions/range/time_range.rb +1 -1
- data/lib/polars/functions/repeat.rb +3 -12
- data/lib/polars/functions/whenthen.rb +2 -2
- data/lib/polars/group_by.rb +72 -20
- data/lib/polars/iceberg_dataset.rb +1 -6
- data/lib/polars/in_process_query.rb +37 -0
- data/lib/polars/io/cloud.rb +18 -0
- data/lib/polars/io/csv.rb +265 -126
- data/lib/polars/io/database.rb +0 -1
- data/lib/polars/io/delta.rb +15 -7
- data/lib/polars/io/ipc.rb +24 -17
- data/lib/polars/io/ndjson.rb +161 -24
- data/lib/polars/io/parquet.rb +101 -38
- data/lib/polars/lazy_frame.rb +849 -558
- data/lib/polars/lazy_group_by.rb +327 -2
- data/lib/polars/list_expr.rb +94 -16
- data/lib/polars/list_name_space.rb +88 -24
- data/lib/polars/meta_expr.rb +42 -1
- data/lib/polars/name_expr.rb +41 -4
- data/lib/polars/query_opt_flags.rb +198 -2
- data/lib/polars/rolling_group_by.rb +3 -3
- data/lib/polars/schema.rb +21 -3
- data/lib/polars/selector.rb +37 -2
- data/lib/polars/selectors.rb +45 -9
- data/lib/polars/series.rb +1156 -728
- data/lib/polars/series_plot.rb +72 -0
- data/lib/polars/slice.rb +1 -1
- data/lib/polars/sql_context.rb +11 -4
- data/lib/polars/string_expr.rb +59 -68
- data/lib/polars/string_name_space.rb +51 -87
- data/lib/polars/struct_expr.rb +36 -18
- 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 +364 -0
- data/lib/polars/utils/construction/utils.rb +9 -0
- data/lib/polars/utils/deprecation.rb +11 -0
- data/lib/polars/utils/serde.rb +8 -3
- data/lib/polars/utils/unstable.rb +19 -0
- data/lib/polars/utils/various.rb +59 -0
- data/lib/polars/utils.rb +46 -47
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +47 -1
- metadata +25 -6
- data/ext/polars/src/allocator.rs +0 -13
- data/lib/polars/plot.rb +0 -109
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
module Polars
|
|
2
|
+
# DataFrame.plot namespace.
|
|
3
|
+
class DataFramePlot
|
|
4
|
+
# @private
|
|
5
|
+
def initialize(df)
|
|
6
|
+
require "vega"
|
|
7
|
+
|
|
8
|
+
@df = df
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Draw line plot.
|
|
12
|
+
#
|
|
13
|
+
# @param x [String]
|
|
14
|
+
# Column with x-coordinates of lines.
|
|
15
|
+
# @param y [String]
|
|
16
|
+
# Column with y-coordinates of lines.
|
|
17
|
+
# @param color [String]
|
|
18
|
+
# Column to color lines by.
|
|
19
|
+
#
|
|
20
|
+
# @return [Vega::LiteChart]
|
|
21
|
+
def line(x, y, color: nil, _type: "line")
|
|
22
|
+
data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)
|
|
23
|
+
|
|
24
|
+
x_type =
|
|
25
|
+
if @df[x].dtype.numeric?
|
|
26
|
+
"quantitative"
|
|
27
|
+
elsif @df[x].dtype.temporal?
|
|
28
|
+
"temporal"
|
|
29
|
+
else
|
|
30
|
+
"nominal"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
scale = x_type == "temporal" ? {type: "utc"} : {}
|
|
34
|
+
encoding = {
|
|
35
|
+
x: {field: x, type: x_type, scale: scale},
|
|
36
|
+
y: {field: y, type: "quantitative"}
|
|
37
|
+
}
|
|
38
|
+
encoding[:color] = {field: color} if color
|
|
39
|
+
|
|
40
|
+
Vega.lite
|
|
41
|
+
.data(data)
|
|
42
|
+
.mark(type: _type, tooltip: true, interpolate: "cardinal", point: {size: 60})
|
|
43
|
+
.encoding(encoding)
|
|
44
|
+
.config(axis: {labelFontSize: 12})
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Draw area plot.
|
|
48
|
+
#
|
|
49
|
+
# @param x [String]
|
|
50
|
+
# Column with x-coordinates of lines.
|
|
51
|
+
# @param y [String]
|
|
52
|
+
# Column with y-coordinates of lines.
|
|
53
|
+
# @param color [String]
|
|
54
|
+
# Column to color lines by.
|
|
55
|
+
#
|
|
56
|
+
# @return [Vega::LiteChart]
|
|
57
|
+
def area(x, y, color: nil)
|
|
58
|
+
line(x, y, color: color, _type: "area")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Draw pie chart.
|
|
62
|
+
#
|
|
63
|
+
# @param x [String]
|
|
64
|
+
# Column with label of slice.
|
|
65
|
+
# @param y [String]
|
|
66
|
+
# Column with size of slice.
|
|
67
|
+
#
|
|
68
|
+
# @return [Vega::LiteChart]
|
|
69
|
+
def pie(x, y)
|
|
70
|
+
data = @df[[x, y].map(&:to_s).uniq].rows(named: true)
|
|
71
|
+
|
|
72
|
+
Vega.lite
|
|
73
|
+
.data(data)
|
|
74
|
+
.mark(type: "arc", tooltip: true)
|
|
75
|
+
.encoding(
|
|
76
|
+
color: {field: x, type: "nominal", sort: "none", axis: {title: nil}, legend: {labelFontSize: 12}},
|
|
77
|
+
theta: {field: y, type: "quantitative"}
|
|
78
|
+
)
|
|
79
|
+
.view(stroke: nil)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Draw column plot.
|
|
83
|
+
#
|
|
84
|
+
# @param x [String]
|
|
85
|
+
# Column with x-coordinates of columns.
|
|
86
|
+
# @param y [String]
|
|
87
|
+
# Column with y-coordinates of columns.
|
|
88
|
+
# @param color [String]
|
|
89
|
+
# Column to color columns by.
|
|
90
|
+
# @param stacked [Boolean]
|
|
91
|
+
# Stack columns.
|
|
92
|
+
#
|
|
93
|
+
# @return [Vega::LiteChart]
|
|
94
|
+
def column(x, y, color: nil, stacked: nil)
|
|
95
|
+
data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)
|
|
96
|
+
|
|
97
|
+
encoding = {
|
|
98
|
+
x: {field: x, type: "nominal", sort: "none", axis: {labelAngle: 0}},
|
|
99
|
+
y: {field: y, type: "quantitative"}
|
|
100
|
+
}
|
|
101
|
+
if color
|
|
102
|
+
encoding[:color] = {field: color}
|
|
103
|
+
encoding[:xOffset] = {field: color} unless stacked
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
Vega.lite
|
|
107
|
+
.data(data)
|
|
108
|
+
.mark(type: "bar", tooltip: true)
|
|
109
|
+
.encoding(encoding)
|
|
110
|
+
.config(axis: {labelFontSize: 12})
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Draw bar plot.
|
|
114
|
+
#
|
|
115
|
+
# @param x [String]
|
|
116
|
+
# Column with x-coordinates of bars.
|
|
117
|
+
# @param y [String]
|
|
118
|
+
# Column with y-coordinates of bars.
|
|
119
|
+
# @param color [String]
|
|
120
|
+
# Column to color bars by.
|
|
121
|
+
# @param stacked [Boolean]
|
|
122
|
+
# Stack bars.
|
|
123
|
+
#
|
|
124
|
+
# @return [Vega::LiteChart]
|
|
125
|
+
def bar(x, y, color: nil, stacked: nil)
|
|
126
|
+
data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)
|
|
127
|
+
|
|
128
|
+
encoding = {
|
|
129
|
+
# TODO determine label angle
|
|
130
|
+
y: {field: x, type: "nominal", sort: "none", axis: {labelAngle: 0}},
|
|
131
|
+
x: {field: y, type: "quantitative"}
|
|
132
|
+
}
|
|
133
|
+
if color
|
|
134
|
+
encoding[:color] = {field: color}
|
|
135
|
+
encoding[:yOffset] = {field: color} unless stacked
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
Vega.lite
|
|
139
|
+
.data(data)
|
|
140
|
+
.mark(type: "bar", tooltip: true)
|
|
141
|
+
.encoding(encoding)
|
|
142
|
+
.config(axis: {labelFontSize: 12})
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Draw scatter plot.
|
|
146
|
+
#
|
|
147
|
+
# @param x [String]
|
|
148
|
+
# Column with x-coordinates of points.
|
|
149
|
+
# @param y [String]
|
|
150
|
+
# Column with y-coordinates of points.
|
|
151
|
+
# @param color [String]
|
|
152
|
+
# Column to color points by.
|
|
153
|
+
#
|
|
154
|
+
# @return [Vega::LiteChart]
|
|
155
|
+
def scatter(x, y, color: nil)
|
|
156
|
+
data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)
|
|
157
|
+
|
|
158
|
+
encoding = {
|
|
159
|
+
x: {field: x, type: "quantitative", scale: {zero: false}},
|
|
160
|
+
y: {field: y, type: "quantitative", scale: {zero: false}},
|
|
161
|
+
size: {value: 60}
|
|
162
|
+
}
|
|
163
|
+
encoding[:color] = {field: color} if color
|
|
164
|
+
|
|
165
|
+
Vega.lite
|
|
166
|
+
.data(data)
|
|
167
|
+
.mark(type: "circle", tooltip: true)
|
|
168
|
+
.encoding(encoding)
|
|
169
|
+
.config(axis: {labelFontSize: 12})
|
|
170
|
+
end
|
|
171
|
+
alias_method :point, :scatter
|
|
172
|
+
end
|
|
173
|
+
end
|
data/lib/polars/data_types.rb
CHANGED
|
@@ -278,7 +278,6 @@ module Polars
|
|
|
278
278
|
# Calendar date and time type.
|
|
279
279
|
class Datetime < TemporalType
|
|
280
280
|
attr_reader :time_unit, :time_zone
|
|
281
|
-
alias_method :tu, :time_unit
|
|
282
281
|
|
|
283
282
|
def initialize(time_unit = "us", time_zone = nil)
|
|
284
283
|
@time_unit = time_unit || "us"
|
|
@@ -303,7 +302,6 @@ module Polars
|
|
|
303
302
|
# Time duration/delta type.
|
|
304
303
|
class Duration < TemporalType
|
|
305
304
|
attr_reader :time_unit
|
|
306
|
-
alias_method :tu, :time_unit
|
|
307
305
|
|
|
308
306
|
def initialize(time_unit = "us")
|
|
309
307
|
@time_unit = time_unit
|
|
@@ -443,7 +441,7 @@ module Polars
|
|
|
443
441
|
attr_reader :inner
|
|
444
442
|
|
|
445
443
|
def initialize(inner)
|
|
446
|
-
@inner = Utils.
|
|
444
|
+
@inner = Utils.parse_into_dtype(inner)
|
|
447
445
|
end
|
|
448
446
|
|
|
449
447
|
def ==(other)
|
|
@@ -463,21 +461,42 @@ module Polars
|
|
|
463
461
|
|
|
464
462
|
# Nested list/array type.
|
|
465
463
|
class Array < NestedType
|
|
466
|
-
attr_reader :inner, :
|
|
464
|
+
attr_reader :inner, :size, :shape
|
|
467
465
|
|
|
468
|
-
def initialize(inner,
|
|
469
|
-
if
|
|
470
|
-
|
|
466
|
+
def initialize(inner, shape)
|
|
467
|
+
if shape.nil?
|
|
468
|
+
msg = "Array constructor is missing the required argument `shape`"
|
|
469
|
+
raise TypeError, msg
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
inner_parsed = Utils.parse_into_dtype(inner)
|
|
473
|
+
inner_shape = inner_parsed.is_a?(Array) ? inner_parsed.shape : []
|
|
474
|
+
|
|
475
|
+
if shape.is_a?(Integer)
|
|
476
|
+
@inner = inner_parsed
|
|
477
|
+
@size = shape
|
|
478
|
+
@shape = [shape] + inner_shape
|
|
479
|
+
|
|
480
|
+
elsif shape.is_a?(::Array) && shape[0].is_a?(Integer)
|
|
481
|
+
if shape.length > 1
|
|
482
|
+
inner_parsed = Array.new(inner_parsed, shape[1..])
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
@inner = inner_parsed
|
|
486
|
+
@size = shape[0]
|
|
487
|
+
@shape = shape + inner_shape
|
|
488
|
+
|
|
489
|
+
else
|
|
490
|
+
msg = "invalid input for shape: #{shape.inspect}"
|
|
491
|
+
raise TypeError, msg
|
|
471
492
|
end
|
|
472
|
-
@inner = Utils.rb_type_to_dtype(inner) if inner
|
|
473
|
-
@width = width
|
|
474
493
|
end
|
|
475
494
|
|
|
476
495
|
def ==(other)
|
|
477
496
|
if other.eql?(Array)
|
|
478
497
|
true
|
|
479
498
|
elsif other.is_a?(Array)
|
|
480
|
-
if @
|
|
499
|
+
if @shape != other.shape
|
|
481
500
|
false
|
|
482
501
|
elsif @inner.nil? || other.inner.nil?
|
|
483
502
|
true
|
|
@@ -490,7 +509,7 @@ module Polars
|
|
|
490
509
|
end
|
|
491
510
|
|
|
492
511
|
def to_s
|
|
493
|
-
"#{self.class.name}(#{inner},
|
|
512
|
+
"#{self.class.name}(#{inner}, shape: #{shape.inspect})"
|
|
494
513
|
end
|
|
495
514
|
end
|
|
496
515
|
|
|
@@ -500,7 +519,7 @@ module Polars
|
|
|
500
519
|
|
|
501
520
|
def initialize(name, dtype)
|
|
502
521
|
@name = name
|
|
503
|
-
@dtype = Utils.
|
|
522
|
+
@dtype = Utils.parse_into_dtype(dtype)
|
|
504
523
|
end
|
|
505
524
|
|
|
506
525
|
def ==(other)
|
|
@@ -353,7 +353,7 @@ module Polars
|
|
|
353
353
|
#
|
|
354
354
|
# @return [Expr]
|
|
355
355
|
def combine(time, time_unit: "us")
|
|
356
|
-
unless time.is_a?(Time) || time.is_a?(Expr)
|
|
356
|
+
unless time.is_a?(::Time) || time.is_a?(Expr)
|
|
357
357
|
raise TypeError, "expected 'time' to be a Ruby time or Polars expression, found #{time}"
|
|
358
358
|
end
|
|
359
359
|
time = Utils.parse_into_expression(time)
|
|
@@ -398,13 +398,21 @@ module Polars
|
|
|
398
398
|
# # │ 2020-04-01 00:00:00 ┆ 2020/04/01 00:00:00 │
|
|
399
399
|
# # │ 2020-05-01 00:00:00 ┆ 2020/05/01 00:00:00 │
|
|
400
400
|
# # └─────────────────────┴─────────────────────┘
|
|
401
|
-
def to_string(format)
|
|
401
|
+
def to_string(format = nil)
|
|
402
|
+
if format.nil?
|
|
403
|
+
format = "iso"
|
|
404
|
+
end
|
|
402
405
|
Utils.wrap_expr(_rbexpr.dt_to_string(format))
|
|
403
406
|
end
|
|
404
407
|
|
|
405
|
-
#
|
|
408
|
+
# Convert a Date/Time/Datetime column into a String column with the given format.
|
|
409
|
+
#
|
|
410
|
+
# Similar to `cast(Polars::String)`, but this method allows you to customize the
|
|
411
|
+
# formatting of the resulting string.
|
|
406
412
|
#
|
|
407
|
-
#
|
|
413
|
+
# @param format [String]
|
|
414
|
+
# Format to use, refer to the [chrono strftime documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)
|
|
415
|
+
# for specification. Example: `"%y-%m-%d"`.
|
|
408
416
|
#
|
|
409
417
|
# @return [Expr]
|
|
410
418
|
#
|
|
@@ -451,8 +459,8 @@ module Polars
|
|
|
451
459
|
# # │ 2020-04-01 00:00:00 ┆ Wednesday ┆ April │
|
|
452
460
|
# # │ 2020-05-01 00:00:00 ┆ Friday ┆ May │
|
|
453
461
|
# # └─────────────────────┴───────────┴────────────┘
|
|
454
|
-
def strftime(
|
|
455
|
-
Utils.wrap_expr(_rbexpr.strftime(
|
|
462
|
+
def strftime(format)
|
|
463
|
+
Utils.wrap_expr(_rbexpr.strftime(format))
|
|
456
464
|
end
|
|
457
465
|
|
|
458
466
|
# Extract the millennium from underlying representation.
|
|
@@ -717,6 +725,35 @@ module Polars
|
|
|
717
725
|
Utils.wrap_expr(_rbexpr.dt_month)
|
|
718
726
|
end
|
|
719
727
|
|
|
728
|
+
# Extract the number of days in the month from the underlying Date representation.
|
|
729
|
+
#
|
|
730
|
+
# Applies to Date and Datetime columns.
|
|
731
|
+
#
|
|
732
|
+
# Returns the number of days in the month.
|
|
733
|
+
# The return value ranges from 28 to 31.
|
|
734
|
+
#
|
|
735
|
+
# @return [Expr]
|
|
736
|
+
#
|
|
737
|
+
# @example
|
|
738
|
+
# df = Polars::DataFrame.new(
|
|
739
|
+
# {"date" => [Date.new(2001, 1, 1), Date.new(2001, 2, 1), Date.new(2000, 2, 1)]}
|
|
740
|
+
# )
|
|
741
|
+
# df.with_columns(Polars.col("date").dt.days_in_month.alias("days_in_month"))
|
|
742
|
+
# # =>
|
|
743
|
+
# # shape: (3, 2)
|
|
744
|
+
# # ┌────────────┬───────────────┐
|
|
745
|
+
# # │ date ┆ days_in_month │
|
|
746
|
+
# # │ --- ┆ --- │
|
|
747
|
+
# # │ date ┆ i8 │
|
|
748
|
+
# # ╞════════════╪═══════════════╡
|
|
749
|
+
# # │ 2001-01-01 ┆ 31 │
|
|
750
|
+
# # │ 2001-02-01 ┆ 28 │
|
|
751
|
+
# # │ 2000-02-01 ┆ 29 │
|
|
752
|
+
# # └────────────┴───────────────┘
|
|
753
|
+
def days_in_month
|
|
754
|
+
Utils.wrap_expr(_rbexpr.dt_days_in_month)
|
|
755
|
+
end
|
|
756
|
+
|
|
720
757
|
# Extract the week from the underlying Date representation.
|
|
721
758
|
#
|
|
722
759
|
# Applies to Date and Datetime columns.
|
|
@@ -896,13 +933,6 @@ module Polars
|
|
|
896
933
|
Utils.wrap_expr(_rbexpr.dt_date)
|
|
897
934
|
end
|
|
898
935
|
|
|
899
|
-
# Datetime
|
|
900
|
-
#
|
|
901
|
-
# @return [Expr]
|
|
902
|
-
def datetime
|
|
903
|
-
Utils.wrap_expr(_rbexpr.dt_datetime)
|
|
904
|
-
end
|
|
905
|
-
|
|
906
936
|
# Extract hour from underlying DateTime representation.
|
|
907
937
|
#
|
|
908
938
|
# Applies to Datetime columns.
|
|
@@ -1190,7 +1220,7 @@ module Polars
|
|
|
1190
1220
|
elsif time_unit == "s"
|
|
1191
1221
|
timestamp("ms").floordiv(F.lit(1000, dtype: Int64))
|
|
1192
1222
|
elsif time_unit == "d"
|
|
1193
|
-
Utils.wrap_expr(_rbexpr).cast(
|
|
1223
|
+
Utils.wrap_expr(_rbexpr).cast(Date).cast(Int32)
|
|
1194
1224
|
else
|
|
1195
1225
|
raise ArgumentError, "time_unit must be one of {'ns', 'us', 'ms', 's', 'd'}, got #{time_unit.inspect}"
|
|
1196
1226
|
end
|
|
@@ -1228,47 +1258,6 @@ module Polars
|
|
|
1228
1258
|
Utils.wrap_expr(_rbexpr.dt_timestamp(time_unit))
|
|
1229
1259
|
end
|
|
1230
1260
|
|
|
1231
|
-
# Set time unit of a Series of dtype Datetime or Duration.
|
|
1232
|
-
#
|
|
1233
|
-
# This does not modify underlying data, and should be used to fix an incorrect
|
|
1234
|
-
# time unit.
|
|
1235
|
-
#
|
|
1236
|
-
# @param time_unit ["ns", "us", "ms"]
|
|
1237
|
-
# Time unit for the `Datetime` Series.
|
|
1238
|
-
#
|
|
1239
|
-
# @return [Expr]
|
|
1240
|
-
#
|
|
1241
|
-
# @example
|
|
1242
|
-
# df = Polars::DataFrame.new(
|
|
1243
|
-
# {
|
|
1244
|
-
# "date" => Polars.datetime_range(
|
|
1245
|
-
# Time.utc(2001, 1, 1),
|
|
1246
|
-
# Time.utc(2001, 1, 3),
|
|
1247
|
-
# "1d",
|
|
1248
|
-
# time_unit: "ns",
|
|
1249
|
-
# eager: true
|
|
1250
|
-
# )
|
|
1251
|
-
# }
|
|
1252
|
-
# )
|
|
1253
|
-
# df.select(
|
|
1254
|
-
# Polars.col("date"),
|
|
1255
|
-
# Polars.col("date").dt.with_time_unit("us").alias("time_unit_us")
|
|
1256
|
-
# )
|
|
1257
|
-
# # =>
|
|
1258
|
-
# # shape: (3, 2)
|
|
1259
|
-
# # ┌─────────────────────┬───────────────────────┐
|
|
1260
|
-
# # │ date ┆ time_unit_us │
|
|
1261
|
-
# # │ --- ┆ --- │
|
|
1262
|
-
# # │ datetime[ns] ┆ datetime[μs] │
|
|
1263
|
-
# # ╞═════════════════════╪═══════════════════════╡
|
|
1264
|
-
# # │ 2001-01-01 00:00:00 ┆ +32971-04-28 00:00:00 │
|
|
1265
|
-
# # │ 2001-01-02 00:00:00 ┆ +32974-01-22 00:00:00 │
|
|
1266
|
-
# # │ 2001-01-03 00:00:00 ┆ +32976-10-18 00:00:00 │
|
|
1267
|
-
# # └─────────────────────┴───────────────────────┘
|
|
1268
|
-
def with_time_unit(time_unit)
|
|
1269
|
-
Utils.wrap_expr(_rbexpr.dt_with_time_unit(time_unit))
|
|
1270
|
-
end
|
|
1271
|
-
|
|
1272
1261
|
# Cast the underlying data to another time unit. This may lose precision.
|
|
1273
1262
|
#
|
|
1274
1263
|
# @param time_unit ["ns", "us", "ms"]
|
|
@@ -1451,7 +1440,7 @@ module Polars
|
|
|
1451
1440
|
# df.select(
|
|
1452
1441
|
# [
|
|
1453
1442
|
# Polars.col("date"),
|
|
1454
|
-
# Polars.col("date").diff.dt.
|
|
1443
|
+
# Polars.col("date").diff.dt.total_days.alias("days_diff")
|
|
1455
1444
|
# ]
|
|
1456
1445
|
# )
|
|
1457
1446
|
# # =>
|
|
@@ -1468,7 +1457,6 @@ module Polars
|
|
|
1468
1457
|
def total_days(fractional: false)
|
|
1469
1458
|
Utils.wrap_expr(_rbexpr.dt_total_days(fractional))
|
|
1470
1459
|
end
|
|
1471
|
-
alias_method :days, :total_days
|
|
1472
1460
|
|
|
1473
1461
|
# Extract the hours from a Duration type.
|
|
1474
1462
|
#
|
|
@@ -1488,7 +1476,7 @@ module Polars
|
|
|
1488
1476
|
# df.select(
|
|
1489
1477
|
# [
|
|
1490
1478
|
# Polars.col("date"),
|
|
1491
|
-
# Polars.col("date").diff.dt.
|
|
1479
|
+
# Polars.col("date").diff.dt.total_hours.alias("hours_diff")
|
|
1492
1480
|
# ]
|
|
1493
1481
|
# )
|
|
1494
1482
|
# # =>
|
|
@@ -1506,7 +1494,6 @@ module Polars
|
|
|
1506
1494
|
def total_hours(fractional: false)
|
|
1507
1495
|
Utils.wrap_expr(_rbexpr.dt_total_hours(fractional))
|
|
1508
1496
|
end
|
|
1509
|
-
alias_method :hours, :total_hours
|
|
1510
1497
|
|
|
1511
1498
|
# Extract the minutes from a Duration type.
|
|
1512
1499
|
#
|
|
@@ -1526,7 +1513,7 @@ module Polars
|
|
|
1526
1513
|
# df.select(
|
|
1527
1514
|
# [
|
|
1528
1515
|
# Polars.col("date"),
|
|
1529
|
-
# Polars.col("date").diff.dt.
|
|
1516
|
+
# Polars.col("date").diff.dt.total_minutes.alias("minutes_diff")
|
|
1530
1517
|
# ]
|
|
1531
1518
|
# )
|
|
1532
1519
|
# # =>
|
|
@@ -1544,7 +1531,6 @@ module Polars
|
|
|
1544
1531
|
def total_minutes(fractional: false)
|
|
1545
1532
|
Utils.wrap_expr(_rbexpr.dt_total_minutes(fractional))
|
|
1546
1533
|
end
|
|
1547
|
-
alias_method :minutes, :total_minutes
|
|
1548
1534
|
|
|
1549
1535
|
# Extract the seconds from a Duration type.
|
|
1550
1536
|
#
|
|
@@ -1564,7 +1550,7 @@ module Polars
|
|
|
1564
1550
|
# df.select(
|
|
1565
1551
|
# [
|
|
1566
1552
|
# Polars.col("date"),
|
|
1567
|
-
# Polars.col("date").diff.dt.
|
|
1553
|
+
# Polars.col("date").diff.dt.total_seconds.alias("seconds_diff")
|
|
1568
1554
|
# ]
|
|
1569
1555
|
# )
|
|
1570
1556
|
# # =>
|
|
@@ -1583,7 +1569,6 @@ module Polars
|
|
|
1583
1569
|
def total_seconds(fractional: false)
|
|
1584
1570
|
Utils.wrap_expr(_rbexpr.dt_total_seconds(fractional))
|
|
1585
1571
|
end
|
|
1586
|
-
alias_method :seconds, :total_seconds
|
|
1587
1572
|
|
|
1588
1573
|
# Extract the milliseconds from a Duration type.
|
|
1589
1574
|
#
|
|
@@ -1603,7 +1588,7 @@ module Polars
|
|
|
1603
1588
|
# df.select(
|
|
1604
1589
|
# [
|
|
1605
1590
|
# Polars.col("date"),
|
|
1606
|
-
# Polars.col("date").diff.dt.
|
|
1591
|
+
# Polars.col("date").diff.dt.total_milliseconds.alias("milliseconds_diff")
|
|
1607
1592
|
# ]
|
|
1608
1593
|
# )
|
|
1609
1594
|
# # =>
|
|
@@ -1628,7 +1613,6 @@ module Polars
|
|
|
1628
1613
|
def total_milliseconds(fractional: false)
|
|
1629
1614
|
Utils.wrap_expr(_rbexpr.dt_total_milliseconds(fractional))
|
|
1630
1615
|
end
|
|
1631
|
-
alias_method :milliseconds, :total_milliseconds
|
|
1632
1616
|
|
|
1633
1617
|
# Extract the microseconds from a Duration type.
|
|
1634
1618
|
#
|
|
@@ -1648,7 +1632,7 @@ module Polars
|
|
|
1648
1632
|
# df.select(
|
|
1649
1633
|
# [
|
|
1650
1634
|
# Polars.col("date"),
|
|
1651
|
-
# Polars.col("date").diff.dt.
|
|
1635
|
+
# Polars.col("date").diff.dt.total_microseconds.alias("microseconds_diff")
|
|
1652
1636
|
# ]
|
|
1653
1637
|
# )
|
|
1654
1638
|
# # =>
|
|
@@ -1673,7 +1657,6 @@ module Polars
|
|
|
1673
1657
|
def total_microseconds(fractional: false)
|
|
1674
1658
|
Utils.wrap_expr(_rbexpr.dt_total_microseconds(fractional))
|
|
1675
1659
|
end
|
|
1676
|
-
alias_method :microseconds, :total_microseconds
|
|
1677
1660
|
|
|
1678
1661
|
# Extract the nanoseconds from a Duration type.
|
|
1679
1662
|
#
|
|
@@ -1693,7 +1676,7 @@ module Polars
|
|
|
1693
1676
|
# df.select(
|
|
1694
1677
|
# [
|
|
1695
1678
|
# Polars.col("date"),
|
|
1696
|
-
# Polars.col("date").diff.dt.
|
|
1679
|
+
# Polars.col("date").diff.dt.total_nanoseconds.alias("nanoseconds_diff")
|
|
1697
1680
|
# ]
|
|
1698
1681
|
# )
|
|
1699
1682
|
# # =>
|
|
@@ -1718,7 +1701,6 @@ module Polars
|
|
|
1718
1701
|
def total_nanoseconds(fractional: false)
|
|
1719
1702
|
Utils.wrap_expr(_rbexpr.dt_total_nanoseconds(fractional))
|
|
1720
1703
|
end
|
|
1721
|
-
alias_method :nanoseconds, :total_nanoseconds
|
|
1722
1704
|
|
|
1723
1705
|
# Offset this date by a relative time offset.
|
|
1724
1706
|
#
|