polars-df 0.10.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/Cargo.lock +392 -351
- data/README.md +6 -6
- data/ext/polars/Cargo.toml +12 -7
- data/ext/polars/src/batched_csv.rs +53 -52
- data/ext/polars/src/conversion/any_value.rs +261 -0
- data/ext/polars/src/conversion/chunked_array.rs +4 -4
- data/ext/polars/src/conversion/mod.rs +60 -66
- data/ext/polars/src/dataframe/construction.rs +184 -0
- data/ext/polars/src/dataframe/export.rs +48 -0
- data/ext/polars/src/dataframe/general.rs +597 -0
- data/ext/polars/src/dataframe/io.rs +473 -0
- data/ext/polars/src/dataframe/mod.rs +26 -0
- data/ext/polars/src/error.rs +26 -4
- data/ext/polars/src/expr/categorical.rs +0 -10
- data/ext/polars/src/expr/datetime.rs +4 -8
- data/ext/polars/src/expr/general.rs +129 -94
- data/ext/polars/src/expr/mod.rs +2 -2
- data/ext/polars/src/expr/rolling.rs +201 -77
- data/ext/polars/src/expr/string.rs +11 -36
- data/ext/polars/src/functions/eager.rs +10 -10
- data/ext/polars/src/functions/lazy.rs +23 -21
- data/ext/polars/src/functions/range.rs +69 -1
- data/ext/polars/src/interop/mod.rs +1 -0
- data/ext/polars/src/interop/numo/mod.rs +2 -0
- data/ext/polars/src/interop/numo/to_numo_df.rs +23 -0
- data/ext/polars/src/interop/numo/to_numo_series.rs +61 -0
- data/ext/polars/src/lazyframe/mod.rs +135 -136
- data/ext/polars/src/lib.rs +94 -59
- data/ext/polars/src/map/dataframe.rs +2 -2
- data/ext/polars/src/map/lazy.rs +5 -25
- data/ext/polars/src/map/series.rs +7 -1
- data/ext/polars/src/rb_modules.rs +25 -1
- data/ext/polars/src/series/aggregation.rs +49 -30
- data/ext/polars/src/series/arithmetic.rs +21 -11
- data/ext/polars/src/series/construction.rs +56 -38
- data/ext/polars/src/series/export.rs +131 -49
- data/ext/polars/src/series/mod.rs +32 -141
- data/ext/polars/src/sql.rs +3 -1
- data/lib/polars/array_expr.rb +4 -4
- data/lib/polars/batched_csv_reader.rb +11 -5
- data/lib/polars/cat_expr.rb +0 -36
- data/lib/polars/cat_name_space.rb +0 -37
- data/lib/polars/convert.rb +6 -1
- data/lib/polars/data_frame.rb +176 -403
- data/lib/polars/data_types.rb +1 -1
- data/lib/polars/date_time_expr.rb +525 -572
- data/lib/polars/date_time_name_space.rb +263 -460
- data/lib/polars/dynamic_group_by.rb +5 -5
- data/lib/polars/exceptions.rb +7 -0
- data/lib/polars/expr.rb +1394 -243
- data/lib/polars/expr_dispatch.rb +1 -1
- data/lib/polars/functions/aggregation/horizontal.rb +8 -8
- data/lib/polars/functions/as_datatype.rb +63 -40
- data/lib/polars/functions/lazy.rb +63 -14
- data/lib/polars/functions/lit.rb +1 -1
- data/lib/polars/functions/range/date_range.rb +90 -57
- data/lib/polars/functions/range/datetime_range.rb +149 -0
- data/lib/polars/functions/range/int_range.rb +2 -2
- data/lib/polars/functions/range/time_range.rb +141 -0
- data/lib/polars/functions/repeat.rb +1 -1
- data/lib/polars/functions/whenthen.rb +1 -1
- data/lib/polars/group_by.rb +88 -23
- data/lib/polars/io/avro.rb +24 -0
- data/lib/polars/{io.rb → io/csv.rb} +299 -493
- data/lib/polars/io/database.rb +73 -0
- data/lib/polars/io/ipc.rb +247 -0
- data/lib/polars/io/json.rb +29 -0
- data/lib/polars/io/ndjson.rb +80 -0
- data/lib/polars/io/parquet.rb +227 -0
- data/lib/polars/lazy_frame.rb +143 -272
- data/lib/polars/lazy_group_by.rb +100 -3
- data/lib/polars/list_expr.rb +11 -11
- data/lib/polars/list_name_space.rb +5 -1
- data/lib/polars/rolling_group_by.rb +7 -9
- data/lib/polars/series.rb +103 -187
- data/lib/polars/string_expr.rb +78 -102
- data/lib/polars/string_name_space.rb +5 -4
- data/lib/polars/testing.rb +2 -2
- data/lib/polars/utils/constants.rb +9 -0
- data/lib/polars/utils/convert.rb +97 -0
- data/lib/polars/utils/parse.rb +89 -0
- data/lib/polars/utils/various.rb +76 -0
- data/lib/polars/utils/wrap.rb +19 -0
- data/lib/polars/utils.rb +8 -300
- data/lib/polars/version.rb +1 -1
- data/lib/polars/whenthen.rb +6 -6
- data/lib/polars.rb +20 -1
- metadata +28 -7
- data/ext/polars/src/conversion/anyvalue.rs +0 -186
- data/ext/polars/src/dataframe.rs +0 -1208
@@ -15,13 +15,11 @@ module Polars
|
|
15
15
|
#
|
16
16
|
# @param every [String]
|
17
17
|
# Every interval start and period length
|
18
|
-
# @param offset [String]
|
19
|
-
# Offset the window
|
20
18
|
#
|
21
19
|
# @return [Expr]
|
22
20
|
#
|
23
21
|
# @note
|
24
|
-
# The `every`
|
22
|
+
# The `every` argument is created with the
|
25
23
|
# the following small string formatting language:
|
26
24
|
#
|
27
25
|
# 1ns # 1 nanosecond
|
@@ -38,17 +36,22 @@ module Polars
|
|
38
36
|
# eg: 3d12h4m25s # 3 days, 12 hours, 4 minutes, and 25 seconds
|
39
37
|
#
|
40
38
|
# @example
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
39
|
+
# df = (
|
40
|
+
# Polars.datetime_range(
|
41
|
+
# DateTime.new(2001, 1, 1),
|
42
|
+
# DateTime.new(2001, 1, 2),
|
43
|
+
# "225m",
|
44
|
+
# eager: true
|
45
|
+
# )
|
46
|
+
# .alias("datetime")
|
47
|
+
# .to_frame
|
48
|
+
# )
|
46
49
|
# # =>
|
47
50
|
# # shape: (7, 1)
|
48
51
|
# # ┌─────────────────────┐
|
49
|
-
# # │
|
52
|
+
# # │ datetime │
|
50
53
|
# # │ --- │
|
51
|
-
# # │ datetime[
|
54
|
+
# # │ datetime[ns] │
|
52
55
|
# # ╞═════════════════════╡
|
53
56
|
# # │ 2001-01-01 00:00:00 │
|
54
57
|
# # │ 2001-01-01 03:45:00 │
|
@@ -60,13 +63,13 @@ module Polars
|
|
60
63
|
# # └─────────────────────┘
|
61
64
|
#
|
62
65
|
# @example
|
63
|
-
# df.select(Polars.col("
|
66
|
+
# df.select(Polars.col("datetime").dt.truncate("1h"))
|
64
67
|
# # =>
|
65
68
|
# # shape: (7, 1)
|
66
69
|
# # ┌─────────────────────┐
|
67
|
-
# # │
|
70
|
+
# # │ datetime │
|
68
71
|
# # │ --- │
|
69
|
-
# # │ datetime[
|
72
|
+
# # │ datetime[ns] │
|
70
73
|
# # ╞═════════════════════╡
|
71
74
|
# # │ 2001-01-01 00:00:00 │
|
72
75
|
# # │ 2001-01-01 03:00:00 │
|
@@ -78,16 +81,20 @@ module Polars
|
|
78
81
|
# # └─────────────────────┘
|
79
82
|
#
|
80
83
|
# @example
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
84
|
+
# df = (
|
85
|
+
# Polars.datetime_range(
|
86
|
+
# DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 1, 1), "10m", eager: true
|
87
|
+
# )
|
88
|
+
# .alias("datetime")
|
89
|
+
# .to_frame
|
90
|
+
# )
|
91
|
+
# df.select(["datetime", Polars.col("datetime").dt.truncate("30m").alias("truncate")])
|
85
92
|
# # =>
|
86
93
|
# # shape: (7, 2)
|
87
94
|
# # ┌─────────────────────┬─────────────────────┐
|
88
|
-
# # │
|
95
|
+
# # │ datetime ┆ truncate │
|
89
96
|
# # │ --- ┆ --- │
|
90
|
-
# # │ datetime[
|
97
|
+
# # │ datetime[ns] ┆ datetime[ns] │
|
91
98
|
# # ╞═════════════════════╪═════════════════════╡
|
92
99
|
# # │ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
|
93
100
|
# # │ 2001-01-01 00:10:00 ┆ 2001-01-01 00:00:00 │
|
@@ -97,22 +104,13 @@ module Polars
|
|
97
104
|
# # │ 2001-01-01 00:50:00 ┆ 2001-01-01 00:30:00 │
|
98
105
|
# # │ 2001-01-01 01:00:00 ┆ 2001-01-01 01:00:00 │
|
99
106
|
# # └─────────────────────┴─────────────────────┘
|
100
|
-
def truncate(every
|
101
|
-
if offset.nil?
|
102
|
-
offset = "0ns"
|
103
|
-
end
|
104
|
-
|
107
|
+
def truncate(every)
|
105
108
|
if !every.is_a?(Expr)
|
106
|
-
every = Utils.
|
109
|
+
every = Utils.parse_as_duration_string(every)
|
107
110
|
end
|
108
|
-
every = Utils.parse_as_expression(every, str_as_lit: true)
|
109
111
|
|
110
|
-
Utils.
|
111
|
-
|
112
|
-
every,
|
113
|
-
Utils._timedelta_to_pl_duration(offset),
|
114
|
-
)
|
115
|
-
)
|
112
|
+
every = Utils.parse_into_expression(every, str_as_lit: true)
|
113
|
+
Utils.wrap_expr(_rbexpr.dt_truncate(every))
|
116
114
|
end
|
117
115
|
|
118
116
|
# Divide the date/datetime range into buckets.
|
@@ -124,8 +122,6 @@ module Polars
|
|
124
122
|
#
|
125
123
|
# @param every [String]
|
126
124
|
# Every interval start and period length
|
127
|
-
# @param offset [String]
|
128
|
-
# Offset the window
|
129
125
|
#
|
130
126
|
# @return [Expr]
|
131
127
|
#
|
@@ -151,56 +147,48 @@ module Polars
|
|
151
147
|
# change without it being considered a breaking change.
|
152
148
|
#
|
153
149
|
# @example
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
# # ╞═════════════════════╡
|
166
|
-
# # │ 2001-01-01 00:00:00 │
|
167
|
-
# # │ 2001-01-01 03:45:00 │
|
168
|
-
# # │ 2001-01-01 07:30:00 │
|
169
|
-
# # │ 2001-01-01 11:15:00 │
|
170
|
-
# # │ 2001-01-01 15:00:00 │
|
171
|
-
# # │ 2001-01-01 18:45:00 │
|
172
|
-
# # │ 2001-01-01 22:30:00 │
|
173
|
-
# # └─────────────────────┘
|
174
|
-
#
|
175
|
-
# @example
|
176
|
-
# df.select(Polars.col("dates").dt.round("1h"))
|
150
|
+
# df = (
|
151
|
+
# Polars.datetime_range(
|
152
|
+
# DateTime.new(2001, 1, 1),
|
153
|
+
# DateTime.new(2001, 1, 2),
|
154
|
+
# "225m",
|
155
|
+
# eager: true
|
156
|
+
# )
|
157
|
+
# .alias("datetime")
|
158
|
+
# .to_frame
|
159
|
+
# )
|
160
|
+
# df.with_columns(Polars.col("datetime").dt.round("1h").alias("round"))
|
177
161
|
# # =>
|
178
|
-
# # shape: (7,
|
179
|
-
# #
|
180
|
-
# # │
|
181
|
-
# # │ --- │
|
182
|
-
# # │ datetime[
|
183
|
-
# #
|
184
|
-
# # │ 2001-01-01 00:00:00 │
|
185
|
-
# # │ 2001-01-01 04:00:00 │
|
186
|
-
# # │ 2001-01-01 08:00:00 │
|
187
|
-
# # │ 2001-01-01 11:00:00 │
|
188
|
-
# # │ 2001-01-01 15:00:00 │
|
189
|
-
# # │ 2001-01-01 19:00:00 │
|
190
|
-
# # │ 2001-01-01 23:00:00 │
|
191
|
-
# #
|
162
|
+
# # shape: (7, 2)
|
163
|
+
# # ┌─────────────────────┬─────────────────────┐
|
164
|
+
# # │ datetime ┆ round │
|
165
|
+
# # │ --- ┆ --- │
|
166
|
+
# # │ datetime[ns] ┆ datetime[ns] │
|
167
|
+
# # ╞═════════════════════╪═════════════════════╡
|
168
|
+
# # │ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
|
169
|
+
# # │ 2001-01-01 03:45:00 ┆ 2001-01-01 04:00:00 │
|
170
|
+
# # │ 2001-01-01 07:30:00 ┆ 2001-01-01 08:00:00 │
|
171
|
+
# # │ 2001-01-01 11:15:00 ┆ 2001-01-01 11:00:00 │
|
172
|
+
# # │ 2001-01-01 15:00:00 ┆ 2001-01-01 15:00:00 │
|
173
|
+
# # │ 2001-01-01 18:45:00 ┆ 2001-01-01 19:00:00 │
|
174
|
+
# # │ 2001-01-01 22:30:00 ┆ 2001-01-01 23:00:00 │
|
175
|
+
# # └─────────────────────┴─────────────────────┘
|
192
176
|
#
|
193
177
|
# @example
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
178
|
+
# df = (
|
179
|
+
# Polars.datetime_range(
|
180
|
+
# DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 1, 1), "10m", eager: true
|
181
|
+
# )
|
182
|
+
# .alias("datetime")
|
183
|
+
# .to_frame
|
184
|
+
# )
|
185
|
+
# df.with_columns(Polars.col("datetime").dt.round("30m").alias("round"))
|
198
186
|
# # =>
|
199
187
|
# # shape: (7, 2)
|
200
188
|
# # ┌─────────────────────┬─────────────────────┐
|
201
|
-
# # │
|
189
|
+
# # │ datetime ┆ round │
|
202
190
|
# # │ --- ┆ --- │
|
203
|
-
# # │ datetime[
|
191
|
+
# # │ datetime[ns] ┆ datetime[ns] │
|
204
192
|
# # ╞═════════════════════╪═════════════════════╡
|
205
193
|
# # │ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
|
206
194
|
# # │ 2001-01-01 00:10:00 ┆ 2001-01-01 00:00:00 │
|
@@ -210,17 +198,9 @@ module Polars
|
|
210
198
|
# # │ 2001-01-01 00:50:00 ┆ 2001-01-01 01:00:00 │
|
211
199
|
# # │ 2001-01-01 01:00:00 ┆ 2001-01-01 01:00:00 │
|
212
200
|
# # └─────────────────────┴─────────────────────┘
|
213
|
-
def round(every
|
214
|
-
|
215
|
-
|
216
|
-
end
|
217
|
-
|
218
|
-
Utils.wrap_expr(
|
219
|
-
_rbexpr.dt_round(
|
220
|
-
Utils._timedelta_to_pl_duration(every),
|
221
|
-
Utils._timedelta_to_pl_duration(offset)
|
222
|
-
)
|
223
|
-
)
|
201
|
+
def round(every)
|
202
|
+
every = Utils.parse_into_expression(every, str_as_lit: true)
|
203
|
+
Utils.wrap_expr(_rbexpr.dt_round(every))
|
224
204
|
end
|
225
205
|
|
226
206
|
# Create a naive Datetime from an existing Date/Datetime expression and a Time.
|
@@ -238,8 +218,50 @@ module Polars
|
|
238
218
|
unless time.is_a?(Time) || time.is_a?(Expr)
|
239
219
|
raise TypeError, "expected 'time' to be a Ruby time or Polars expression, found #{time}"
|
240
220
|
end
|
241
|
-
time = Utils.
|
242
|
-
Utils.wrap_expr(_rbexpr.dt_combine(time
|
221
|
+
time = Utils.parse_into_expression(time)
|
222
|
+
Utils.wrap_expr(_rbexpr.dt_combine(time, time_unit))
|
223
|
+
end
|
224
|
+
|
225
|
+
# Convert a Date/Time/Datetime column into a String column with the given format.
|
226
|
+
#
|
227
|
+
# Similar to `cast(Polars::String)`, but this method allows you to customize the
|
228
|
+
# formatting of the resulting string.
|
229
|
+
#
|
230
|
+
# @param format [String]
|
231
|
+
# Format to use, refer to the `chrono strftime documentation
|
232
|
+
# <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>`_
|
233
|
+
# for specification. Example: `"%y-%m-%d"`.
|
234
|
+
#
|
235
|
+
# @return [Expr]
|
236
|
+
#
|
237
|
+
# @example
|
238
|
+
# df = Polars::DataFrame.new(
|
239
|
+
# {
|
240
|
+
# "datetime" => [
|
241
|
+
# DateTime.new(2020, 3, 1),
|
242
|
+
# DateTime.new(2020, 4, 1),
|
243
|
+
# DateTime.new(2020, 5, 1)
|
244
|
+
# ]
|
245
|
+
# }
|
246
|
+
# )
|
247
|
+
# df.with_columns(
|
248
|
+
# Polars.col("datetime")
|
249
|
+
# .dt.to_string("%Y/%m/%d %H:%M:%S")
|
250
|
+
# .alias("datetime_string")
|
251
|
+
# )
|
252
|
+
# # =>
|
253
|
+
# # shape: (3, 2)
|
254
|
+
# # ┌─────────────────────┬─────────────────────┐
|
255
|
+
# # │ datetime ┆ datetime_string │
|
256
|
+
# # │ --- ┆ --- │
|
257
|
+
# # │ datetime[ns] ┆ str │
|
258
|
+
# # ╞═════════════════════╪═════════════════════╡
|
259
|
+
# # │ 2020-03-01 00:00:00 ┆ 2020/03/01 00:00:00 │
|
260
|
+
# # │ 2020-04-01 00:00:00 ┆ 2020/04/01 00:00:00 │
|
261
|
+
# # │ 2020-05-01 00:00:00 ┆ 2020/05/01 00:00:00 │
|
262
|
+
# # └─────────────────────┴─────────────────────┘
|
263
|
+
def to_string(format)
|
264
|
+
Utils.wrap_expr(_rbexpr.dt_to_string(format))
|
243
265
|
end
|
244
266
|
|
245
267
|
# Format Date/datetime with a formatting rule.
|
@@ -260,38 +282,26 @@ module Polars
|
|
260
282
|
# @return [Expr]
|
261
283
|
#
|
262
284
|
# @example
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
267
|
-
#
|
268
|
-
#
|
269
|
-
#
|
270
|
-
# # │ --- │
|
271
|
-
# # │ datetime[μs] │
|
272
|
-
# # ╞═════════════════════╡
|
273
|
-
# # │ 2001-01-01 00:00:00 │
|
274
|
-
# # │ 2001-06-30 00:00:00 │
|
275
|
-
# # │ 2001-12-27 00:00:00 │
|
276
|
-
# # │ 2002-06-25 00:00:00 │
|
277
|
-
# # └─────────────────────┘
|
278
|
-
#
|
279
|
-
# @example
|
280
|
-
# df.select(Polars.col("date").dt.year)
|
285
|
+
# df = Polars::DataFrame.new(
|
286
|
+
# {"date" => [Date.new(1977, 1, 1), Date.new(1978, 1, 1), Date.new(1979, 1, 1)]}
|
287
|
+
# )
|
288
|
+
# df.with_columns(
|
289
|
+
# calendar_year: Polars.col("date").dt.year,
|
290
|
+
# iso_year: Polars.col("date").dt.iso_year
|
291
|
+
# )
|
281
292
|
# # =>
|
282
|
-
# # shape: (
|
283
|
-
# #
|
284
|
-
# # │ date │
|
285
|
-
# # │ ---
|
286
|
-
# # │ i32
|
287
|
-
# #
|
288
|
-
# # │
|
289
|
-
# # │
|
290
|
-
# # │
|
291
|
-
# #
|
292
|
-
# # └──────┘
|
293
|
+
# # shape: (3, 3)
|
294
|
+
# # ┌────────────┬───────────────┬──────────┐
|
295
|
+
# # │ date ┆ calendar_year ┆ iso_year │
|
296
|
+
# # │ --- ┆ --- ┆ --- │
|
297
|
+
# # │ date ┆ i32 ┆ i32 │
|
298
|
+
# # ╞════════════╪═══════════════╪══════════╡
|
299
|
+
# # │ 1977-01-01 ┆ 1977 ┆ 1976 │
|
300
|
+
# # │ 1978-01-01 ┆ 1978 ┆ 1977 │
|
301
|
+
# # │ 1979-01-01 ┆ 1979 ┆ 1979 │
|
302
|
+
# # └────────────┴───────────────┴──────────┘
|
293
303
|
def year
|
294
|
-
Utils.wrap_expr(_rbexpr.
|
304
|
+
Utils.wrap_expr(_rbexpr.dt_year)
|
295
305
|
end
|
296
306
|
|
297
307
|
# Determine whether the year of the underlying date is a leap year.
|
@@ -301,23 +311,23 @@ module Polars
|
|
301
311
|
# @return [Expr]
|
302
312
|
#
|
303
313
|
# @example
|
304
|
-
# start = DateTime.new(2000, 1, 1)
|
305
|
-
# stop = DateTime.new(2002, 1, 1)
|
306
314
|
# df = Polars::DataFrame.new(
|
307
|
-
# {"date" =>
|
315
|
+
# {"date" => [Date.new(2000, 1, 1), Date.new(2001, 1, 1), Date.new(2002, 1, 1)]}
|
316
|
+
# )
|
317
|
+
# df.with_columns(
|
318
|
+
# leap_year: Polars.col("date").dt.is_leap_year
|
308
319
|
# )
|
309
|
-
# df.select(Polars.col("date").dt.is_leap_year)
|
310
320
|
# # =>
|
311
|
-
# # shape: (3,
|
312
|
-
# #
|
313
|
-
# # │ date
|
314
|
-
# # │ ---
|
315
|
-
# # │ bool
|
316
|
-
# #
|
317
|
-
# # │ true
|
318
|
-
# # │ false
|
319
|
-
# # │ false
|
320
|
-
# #
|
321
|
+
# # shape: (3, 2)
|
322
|
+
# # ┌────────────┬───────────┐
|
323
|
+
# # │ date ┆ leap_year │
|
324
|
+
# # │ --- ┆ --- │
|
325
|
+
# # │ date ┆ bool │
|
326
|
+
# # ╞════════════╪═══════════╡
|
327
|
+
# # │ 2000-01-01 ┆ true │
|
328
|
+
# # │ 2001-01-01 ┆ false │
|
329
|
+
# # │ 2002-01-01 ┆ false │
|
330
|
+
# # └────────────┴───────────┘
|
321
331
|
def is_leap_year
|
322
332
|
Utils.wrap_expr(_rbexpr.dt_is_leap_year)
|
323
333
|
end
|
@@ -330,8 +340,29 @@ module Polars
|
|
330
340
|
# This may not correspond with the calendar year.
|
331
341
|
#
|
332
342
|
# @return [Expr]
|
343
|
+
#
|
344
|
+
# @example
|
345
|
+
# df = Polars::DataFrame.new(
|
346
|
+
# {"date" => [Date.new(1977, 1, 1), Date.new(1978, 1, 1), Date.new(1979, 1, 1)]}
|
347
|
+
# )
|
348
|
+
# df.select(
|
349
|
+
# "date",
|
350
|
+
# Polars.col("date").dt.year.alias("calendar_year"),
|
351
|
+
# Polars.col("date").dt.iso_year.alias("iso_year")
|
352
|
+
# )
|
353
|
+
# # =>
|
354
|
+
# # shape: (3, 3)
|
355
|
+
# # ┌────────────┬───────────────┬──────────┐
|
356
|
+
# # │ date ┆ calendar_year ┆ iso_year │
|
357
|
+
# # │ --- ┆ --- ┆ --- │
|
358
|
+
# # │ date ┆ i32 ┆ i32 │
|
359
|
+
# # ╞════════════╪═══════════════╪══════════╡
|
360
|
+
# # │ 1977-01-01 ┆ 1977 ┆ 1976 │
|
361
|
+
# # │ 1978-01-01 ┆ 1978 ┆ 1977 │
|
362
|
+
# # │ 1979-01-01 ┆ 1979 ┆ 1979 │
|
363
|
+
# # └────────────┴───────────────┴──────────┘
|
333
364
|
def iso_year
|
334
|
-
Utils.wrap_expr(_rbexpr.
|
365
|
+
Utils.wrap_expr(_rbexpr.dt_iso_year)
|
335
366
|
end
|
336
367
|
|
337
368
|
# Extract quarter from underlying Date representation.
|
@@ -343,36 +374,23 @@ module Polars
|
|
343
374
|
# @return [Expr]
|
344
375
|
#
|
345
376
|
# @example
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
# # shape: (3, 1)
|
351
|
-
# # ┌─────────────────────┐
|
352
|
-
# # │ date │
|
353
|
-
# # │ --- │
|
354
|
-
# # │ datetime[μs] │
|
355
|
-
# # ╞═════════════════════╡
|
356
|
-
# # │ 2001-01-01 00:00:00 │
|
357
|
-
# # │ 2001-06-30 00:00:00 │
|
358
|
-
# # │ 2001-12-27 00:00:00 │
|
359
|
-
# # └─────────────────────┘
|
360
|
-
#
|
361
|
-
# @example
|
362
|
-
# df.select(Polars.col("date").dt.quarter)
|
377
|
+
# df = Polars::DataFrame.new(
|
378
|
+
# {"date" => [Date.new(2001, 1, 1), Date.new(2001, 6, 30), Date.new(2001, 12, 27)]}
|
379
|
+
# )
|
380
|
+
# df.with_columns(Polars.col("date").dt.quarter.alias("quarter"))
|
363
381
|
# # =>
|
364
|
-
# # shape: (3,
|
365
|
-
# #
|
366
|
-
# # │ date │
|
367
|
-
# # │ ---
|
368
|
-
# # │ i8
|
369
|
-
# #
|
370
|
-
# # │ 1
|
371
|
-
# # │ 2
|
372
|
-
# # │ 4
|
373
|
-
# #
|
382
|
+
# # shape: (3, 2)
|
383
|
+
# # ┌────────────┬─────────┐
|
384
|
+
# # │ date ┆ quarter │
|
385
|
+
# # │ --- ┆ --- │
|
386
|
+
# # │ date ┆ i8 │
|
387
|
+
# # ╞════════════╪═════════╡
|
388
|
+
# # │ 2001-01-01 ┆ 1 │
|
389
|
+
# # │ 2001-06-30 ┆ 2 │
|
390
|
+
# # │ 2001-12-27 ┆ 4 │
|
391
|
+
# # └────────────┴─────────┘
|
374
392
|
def quarter
|
375
|
-
Utils.wrap_expr(_rbexpr.
|
393
|
+
Utils.wrap_expr(_rbexpr.dt_quarter)
|
376
394
|
end
|
377
395
|
|
378
396
|
# Extract month from underlying Date representation.
|
@@ -385,36 +403,23 @@ module Polars
|
|
385
403
|
# @return [Expr]
|
386
404
|
#
|
387
405
|
# @example
|
388
|
-
#
|
389
|
-
#
|
390
|
-
#
|
391
|
-
#
|
392
|
-
# # shape: (3, 1)
|
393
|
-
# # ┌─────────────────────┐
|
394
|
-
# # │ date │
|
395
|
-
# # │ --- │
|
396
|
-
# # │ datetime[μs] │
|
397
|
-
# # ╞═════════════════════╡
|
398
|
-
# # │ 2001-01-01 00:00:00 │
|
399
|
-
# # │ 2001-02-01 00:00:00 │
|
400
|
-
# # │ 2001-03-04 00:00:00 │
|
401
|
-
# # └─────────────────────┘
|
402
|
-
#
|
403
|
-
# @example
|
404
|
-
# df.select(Polars.col("date").dt.month)
|
406
|
+
# df = Polars::DataFrame.new(
|
407
|
+
# {"date" => [Date.new(2001, 1, 1), Date.new(2001, 6, 30), Date.new(2001, 12, 27)]}
|
408
|
+
# )
|
409
|
+
# df.with_columns(Polars.col("date").dt.month.alias("month"))
|
405
410
|
# # =>
|
406
|
-
# # shape: (3,
|
407
|
-
# #
|
408
|
-
# # │ date │
|
409
|
-
# # │ ---
|
410
|
-
# # │ i8
|
411
|
-
# #
|
412
|
-
# # │ 1
|
413
|
-
# # │
|
414
|
-
# # │
|
415
|
-
# #
|
411
|
+
# # shape: (3, 2)
|
412
|
+
# # ┌────────────┬───────┐
|
413
|
+
# # │ date ┆ month │
|
414
|
+
# # │ --- ┆ --- │
|
415
|
+
# # │ date ┆ i8 │
|
416
|
+
# # ╞════════════╪═══════╡
|
417
|
+
# # │ 2001-01-01 ┆ 1 │
|
418
|
+
# # │ 2001-06-30 ┆ 6 │
|
419
|
+
# # │ 2001-12-27 ┆ 12 │
|
420
|
+
# # └────────────┴───────┘
|
416
421
|
def month
|
417
|
-
Utils.wrap_expr(_rbexpr.
|
422
|
+
Utils.wrap_expr(_rbexpr.dt_month)
|
418
423
|
end
|
419
424
|
|
420
425
|
# Extract the week from the underlying Date representation.
|
@@ -427,36 +432,23 @@ module Polars
|
|
427
432
|
# @return [Expr]
|
428
433
|
#
|
429
434
|
# @example
|
430
|
-
#
|
431
|
-
#
|
432
|
-
#
|
433
|
-
#
|
434
|
-
# # shape: (3, 1)
|
435
|
-
# # ┌─────────────────────┐
|
436
|
-
# # │ date │
|
437
|
-
# # │ --- │
|
438
|
-
# # │ datetime[μs] │
|
439
|
-
# # ╞═════════════════════╡
|
440
|
-
# # │ 2001-01-01 00:00:00 │
|
441
|
-
# # │ 2001-02-01 00:00:00 │
|
442
|
-
# # │ 2001-03-04 00:00:00 │
|
443
|
-
# # └─────────────────────┘
|
444
|
-
#
|
445
|
-
# @example
|
446
|
-
# df.select(Polars.col("date").dt.week)
|
435
|
+
# df = Polars::DataFrame.new(
|
436
|
+
# {"date" => [Date.new(2001, 1, 1), Date.new(2001, 6, 30), Date.new(2001, 12, 27)]}
|
437
|
+
# )
|
438
|
+
# df.with_columns(Polars.col("date").dt.week.alias("week"))
|
447
439
|
# # =>
|
448
|
-
# # shape: (3,
|
449
|
-
# #
|
450
|
-
# # │ date │
|
451
|
-
# # │ --- │
|
452
|
-
# # │ i8 │
|
453
|
-
# #
|
454
|
-
# # │ 1 │
|
455
|
-
# # │
|
456
|
-
# # │
|
457
|
-
# #
|
440
|
+
# # shape: (3, 2)
|
441
|
+
# # ┌────────────┬──────┐
|
442
|
+
# # │ date ┆ week │
|
443
|
+
# # │ --- ┆ --- │
|
444
|
+
# # │ date ┆ i8 │
|
445
|
+
# # ╞════════════╪══════╡
|
446
|
+
# # │ 2001-01-01 ┆ 1 │
|
447
|
+
# # │ 2001-06-30 ┆ 26 │
|
448
|
+
# # │ 2001-12-27 ┆ 52 │
|
449
|
+
# # └────────────┴──────┘
|
458
450
|
def week
|
459
|
-
Utils.wrap_expr(_rbexpr.
|
451
|
+
Utils.wrap_expr(_rbexpr.dt_week)
|
460
452
|
end
|
461
453
|
|
462
454
|
# Extract the week day from the underlying Date representation.
|
@@ -468,42 +460,32 @@ module Polars
|
|
468
460
|
# @return [Expr]
|
469
461
|
#
|
470
462
|
# @example
|
471
|
-
#
|
472
|
-
#
|
473
|
-
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
#
|
479
|
-
#
|
480
|
-
#
|
481
|
-
#
|
482
|
-
# # │ 2001-01-04 00:00:00 │
|
483
|
-
# # │ 2001-01-07 00:00:00 │
|
484
|
-
# # └─────────────────────┘
|
485
|
-
#
|
486
|
-
# @example
|
487
|
-
# df.select(
|
488
|
-
# [
|
489
|
-
# Polars.col("date").dt.weekday.alias("weekday"),
|
490
|
-
# Polars.col("date").dt.day.alias("day_of_month"),
|
491
|
-
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
492
|
-
# ]
|
463
|
+
# df = Polars::DataFrame.new(
|
464
|
+
# {
|
465
|
+
# "date" => Polars.date_range(
|
466
|
+
# Date.new(2001, 12, 22), Date.new(2001, 12, 25), eager: true
|
467
|
+
# )
|
468
|
+
# }
|
469
|
+
# )
|
470
|
+
# df.with_columns(
|
471
|
+
# Polars.col("date").dt.weekday.alias("weekday"),
|
472
|
+
# Polars.col("date").dt.day.alias("day_of_month"),
|
473
|
+
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
493
474
|
# )
|
494
475
|
# # =>
|
495
|
-
# # shape: (
|
496
|
-
# #
|
497
|
-
# # │ weekday ┆ day_of_month ┆ day_of_year │
|
498
|
-
# # │ --- ┆ --- ┆ --- │
|
499
|
-
# # │ i8 ┆ i8 ┆ i16 │
|
500
|
-
# #
|
501
|
-
# # │
|
502
|
-
# # │
|
503
|
-
# # │
|
504
|
-
# #
|
476
|
+
# # shape: (4, 4)
|
477
|
+
# # ┌────────────┬─────────┬──────────────┬─────────────┐
|
478
|
+
# # │ date ┆ weekday ┆ day_of_month ┆ day_of_year │
|
479
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
480
|
+
# # │ date ┆ i8 ┆ i8 ┆ i16 │
|
481
|
+
# # ╞════════════╪═════════╪══════════════╪═════════════╡
|
482
|
+
# # │ 2001-12-22 ┆ 6 ┆ 22 ┆ 356 │
|
483
|
+
# # │ 2001-12-23 ┆ 7 ┆ 23 ┆ 357 │
|
484
|
+
# # │ 2001-12-24 ┆ 1 ┆ 24 ┆ 358 │
|
485
|
+
# # │ 2001-12-25 ┆ 2 ┆ 25 ┆ 359 │
|
486
|
+
# # └────────────┴─────────┴──────────────┴─────────────┘
|
505
487
|
def weekday
|
506
|
-
Utils.wrap_expr(_rbexpr.
|
488
|
+
Utils.wrap_expr(_rbexpr.dt_weekday)
|
507
489
|
end
|
508
490
|
|
509
491
|
# Extract day from underlying Date representation.
|
@@ -516,42 +498,32 @@ module Polars
|
|
516
498
|
# @return [Expr]
|
517
499
|
#
|
518
500
|
# @example
|
519
|
-
#
|
520
|
-
#
|
521
|
-
#
|
522
|
-
#
|
523
|
-
#
|
524
|
-
#
|
525
|
-
#
|
526
|
-
#
|
527
|
-
#
|
528
|
-
#
|
529
|
-
#
|
530
|
-
# # │ 2001-01-04 00:00:00 │
|
531
|
-
# # │ 2001-01-07 00:00:00 │
|
532
|
-
# # └─────────────────────┘
|
533
|
-
#
|
534
|
-
# @example
|
535
|
-
# df.select(
|
536
|
-
# [
|
537
|
-
# Polars.col("date").dt.weekday.alias("weekday"),
|
538
|
-
# Polars.col("date").dt.day.alias("day_of_month"),
|
539
|
-
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
540
|
-
# ]
|
501
|
+
# df = Polars::DataFrame.new(
|
502
|
+
# {
|
503
|
+
# "date" => Polars.date_range(
|
504
|
+
# Date.new(2001, 12, 22), Date.new(2001, 12, 25), eager: true
|
505
|
+
# )
|
506
|
+
# }
|
507
|
+
# )
|
508
|
+
# df.with_columns(
|
509
|
+
# Polars.col("date").dt.weekday.alias("weekday"),
|
510
|
+
# Polars.col("date").dt.day.alias("day_of_month"),
|
511
|
+
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
541
512
|
# )
|
542
513
|
# # =>
|
543
|
-
# # shape: (
|
544
|
-
# #
|
545
|
-
# # │ weekday ┆ day_of_month ┆ day_of_year │
|
546
|
-
# # │ --- ┆ --- ┆ --- │
|
547
|
-
# # │ i8 ┆ i8 ┆ i16 │
|
548
|
-
# #
|
549
|
-
# # │
|
550
|
-
# # │
|
551
|
-
# # │
|
552
|
-
# #
|
514
|
+
# # shape: (4, 4)
|
515
|
+
# # ┌────────────┬─────────┬──────────────┬─────────────┐
|
516
|
+
# # │ date ┆ weekday ┆ day_of_month ┆ day_of_year │
|
517
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
518
|
+
# # │ date ┆ i8 ┆ i8 ┆ i16 │
|
519
|
+
# # ╞════════════╪═════════╪══════════════╪═════════════╡
|
520
|
+
# # │ 2001-12-22 ┆ 6 ┆ 22 ┆ 356 │
|
521
|
+
# # │ 2001-12-23 ┆ 7 ┆ 23 ┆ 357 │
|
522
|
+
# # │ 2001-12-24 ┆ 1 ┆ 24 ┆ 358 │
|
523
|
+
# # │ 2001-12-25 ┆ 2 ┆ 25 ┆ 359 │
|
524
|
+
# # └────────────┴─────────┴──────────────┴─────────────┘
|
553
525
|
def day
|
554
|
-
Utils.wrap_expr(_rbexpr.
|
526
|
+
Utils.wrap_expr(_rbexpr.dt_day)
|
555
527
|
end
|
556
528
|
|
557
529
|
# Extract ordinal day from underlying Date representation.
|
@@ -564,42 +536,32 @@ module Polars
|
|
564
536
|
# @return [Expr]
|
565
537
|
#
|
566
538
|
# @example
|
567
|
-
#
|
568
|
-
#
|
569
|
-
#
|
570
|
-
#
|
571
|
-
#
|
572
|
-
#
|
573
|
-
#
|
574
|
-
#
|
575
|
-
#
|
576
|
-
#
|
577
|
-
#
|
578
|
-
# # │ 2001-01-04 00:00:00 │
|
579
|
-
# # │ 2001-01-07 00:00:00 │
|
580
|
-
# # └─────────────────────┘
|
581
|
-
#
|
582
|
-
# @example
|
583
|
-
# df.select(
|
584
|
-
# [
|
585
|
-
# Polars.col("date").dt.weekday.alias("weekday"),
|
586
|
-
# Polars.col("date").dt.day.alias("day_of_month"),
|
587
|
-
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
588
|
-
# ]
|
539
|
+
# df = Polars::DataFrame.new(
|
540
|
+
# {
|
541
|
+
# "date" => Polars.date_range(
|
542
|
+
# Date.new(2001, 12, 22), Date.new(2001, 12, 25), eager: true
|
543
|
+
# )
|
544
|
+
# }
|
545
|
+
# )
|
546
|
+
# df.with_columns(
|
547
|
+
# Polars.col("date").dt.weekday.alias("weekday"),
|
548
|
+
# Polars.col("date").dt.day.alias("day_of_month"),
|
549
|
+
# Polars.col("date").dt.ordinal_day.alias("day_of_year")
|
589
550
|
# )
|
590
551
|
# # =>
|
591
|
-
# # shape: (
|
592
|
-
# #
|
593
|
-
# # │ weekday ┆ day_of_month ┆ day_of_year │
|
594
|
-
# # │ --- ┆ --- ┆ --- │
|
595
|
-
# # │ i8 ┆ i8 ┆ i16 │
|
596
|
-
# #
|
597
|
-
# # │
|
598
|
-
# # │
|
599
|
-
# # │
|
600
|
-
# #
|
552
|
+
# # shape: (4, 4)
|
553
|
+
# # ┌────────────┬─────────┬──────────────┬─────────────┐
|
554
|
+
# # │ date ┆ weekday ┆ day_of_month ┆ day_of_year │
|
555
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
556
|
+
# # │ date ┆ i8 ┆ i8 ┆ i16 │
|
557
|
+
# # ╞════════════╪═════════╪══════════════╪═════════════╡
|
558
|
+
# # │ 2001-12-22 ┆ 6 ┆ 22 ┆ 356 │
|
559
|
+
# # │ 2001-12-23 ┆ 7 ┆ 23 ┆ 357 │
|
560
|
+
# # │ 2001-12-24 ┆ 1 ┆ 24 ┆ 358 │
|
561
|
+
# # │ 2001-12-25 ┆ 2 ┆ 25 ┆ 359 │
|
562
|
+
# # └────────────┴─────────┴──────────────┴─────────────┘
|
601
563
|
def ordinal_day
|
602
|
-
Utils.wrap_expr(_rbexpr.
|
564
|
+
Utils.wrap_expr(_rbexpr.dt_ordinal_day)
|
603
565
|
end
|
604
566
|
|
605
567
|
# Time
|
@@ -632,36 +594,34 @@ module Polars
|
|
632
594
|
# @return [Expr]
|
633
595
|
#
|
634
596
|
# @example
|
635
|
-
#
|
636
|
-
#
|
637
|
-
#
|
638
|
-
#
|
639
|
-
#
|
640
|
-
#
|
641
|
-
#
|
642
|
-
#
|
643
|
-
#
|
644
|
-
#
|
645
|
-
#
|
646
|
-
#
|
647
|
-
#
|
648
|
-
#
|
649
|
-
#
|
650
|
-
# @example
|
651
|
-
# df.select(Polars.col("date").dt.hour)
|
597
|
+
# df = Polars::DataFrame.new(
|
598
|
+
# {
|
599
|
+
# "datetime" => [
|
600
|
+
# Time.utc(1978, 1, 1, 1, 1, 1, 0),
|
601
|
+
# Time.utc(2024, 10, 13, 5, 30, 14, 500_000),
|
602
|
+
# Time.utc(2065, 1, 1, 10, 20, 30, 60_000)
|
603
|
+
# ]
|
604
|
+
# }
|
605
|
+
# )
|
606
|
+
# df.with_columns(
|
607
|
+
# Polars.col("datetime").dt.hour.alias("hour"),
|
608
|
+
# Polars.col("datetime").dt.minute.alias("minute"),
|
609
|
+
# Polars.col("datetime").dt.second.alias("second"),
|
610
|
+
# Polars.col("datetime").dt.millisecond.alias("millisecond")
|
611
|
+
# )
|
652
612
|
# # =>
|
653
|
-
# # shape: (3,
|
654
|
-
# #
|
655
|
-
# # │
|
656
|
-
# # │ --- │
|
657
|
-
# # │ i8 │
|
658
|
-
# #
|
659
|
-
# # │ 0
|
660
|
-
# # │
|
661
|
-
# # │
|
662
|
-
# #
|
613
|
+
# # shape: (3, 5)
|
614
|
+
# # ┌─────────────────────────┬──────┬────────┬────────┬─────────────┐
|
615
|
+
# # │ datetime ┆ hour ┆ minute ┆ second ┆ millisecond │
|
616
|
+
# # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
|
617
|
+
# # │ datetime[ns] ┆ i8 ┆ i8 ┆ i8 ┆ i32 │
|
618
|
+
# # ╞═════════════════════════╪══════╪════════╪════════╪═════════════╡
|
619
|
+
# # │ 1978-01-01 01:01:01 ┆ 1 ┆ 1 ┆ 1 ┆ 0 │
|
620
|
+
# # │ 2024-10-13 05:30:14.500 ┆ 5 ┆ 30 ┆ 14 ┆ 500 │
|
621
|
+
# # │ 2065-01-01 10:20:30.060 ┆ 10 ┆ 20 ┆ 30 ┆ 60 │
|
622
|
+
# # └─────────────────────────┴──────┴────────┴────────┴─────────────┘
|
663
623
|
def hour
|
664
|
-
Utils.wrap_expr(_rbexpr.
|
624
|
+
Utils.wrap_expr(_rbexpr.dt_hour)
|
665
625
|
end
|
666
626
|
|
667
627
|
# Extract minutes from underlying DateTime representation.
|
@@ -673,36 +633,34 @@ module Polars
|
|
673
633
|
# @return [Expr]
|
674
634
|
#
|
675
635
|
# @example
|
676
|
-
#
|
677
|
-
#
|
678
|
-
#
|
679
|
-
#
|
680
|
-
#
|
681
|
-
#
|
682
|
-
#
|
683
|
-
#
|
684
|
-
#
|
685
|
-
#
|
686
|
-
#
|
687
|
-
#
|
688
|
-
#
|
689
|
-
#
|
690
|
-
#
|
691
|
-
# @example
|
692
|
-
# df.select(Polars.col("date").dt.minute)
|
636
|
+
# df = Polars::DataFrame.new(
|
637
|
+
# {
|
638
|
+
# "datetime" => [
|
639
|
+
# Time.utc(1978, 1, 1, 1, 1, 1, 0),
|
640
|
+
# Time.utc(2024, 10, 13, 5, 30, 14, 500_000),
|
641
|
+
# Time.utc(2065, 1, 1, 10, 20, 30, 60_000)
|
642
|
+
# ]
|
643
|
+
# }
|
644
|
+
# )
|
645
|
+
# df.with_columns(
|
646
|
+
# Polars.col("datetime").dt.hour.alias("hour"),
|
647
|
+
# Polars.col("datetime").dt.minute.alias("minute"),
|
648
|
+
# Polars.col("datetime").dt.second.alias("second"),
|
649
|
+
# Polars.col("datetime").dt.millisecond.alias("millisecond")
|
650
|
+
# )
|
693
651
|
# # =>
|
694
|
-
# # shape: (3,
|
695
|
-
# #
|
696
|
-
# # │
|
697
|
-
# # │ --- │
|
698
|
-
# # │ i8 │
|
699
|
-
# #
|
700
|
-
# # │ 0
|
701
|
-
# # │
|
702
|
-
# # │
|
703
|
-
# #
|
652
|
+
# # shape: (3, 5)
|
653
|
+
# # ┌─────────────────────────┬──────┬────────┬────────┬─────────────┐
|
654
|
+
# # │ datetime ┆ hour ┆ minute ┆ second ┆ millisecond │
|
655
|
+
# # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
|
656
|
+
# # │ datetime[ns] ┆ i8 ┆ i8 ┆ i8 ┆ i32 │
|
657
|
+
# # ╞═════════════════════════╪══════╪════════╪════════╪═════════════╡
|
658
|
+
# # │ 1978-01-01 01:01:01 ┆ 1 ┆ 1 ┆ 1 ┆ 0 │
|
659
|
+
# # │ 2024-10-13 05:30:14.500 ┆ 5 ┆ 30 ┆ 14 ┆ 500 │
|
660
|
+
# # │ 2065-01-01 10:20:30.060 ┆ 10 ┆ 20 ┆ 30 ┆ 60 │
|
661
|
+
# # └─────────────────────────┴──────┴────────┴────────┴─────────────┘
|
704
662
|
def minute
|
705
|
-
Utils.wrap_expr(_rbexpr.
|
663
|
+
Utils.wrap_expr(_rbexpr.dt_minute)
|
706
664
|
end
|
707
665
|
|
708
666
|
# Extract seconds from underlying DateTime representation.
|
@@ -718,87 +676,52 @@ module Polars
|
|
718
676
|
# @example
|
719
677
|
# df = Polars::DataFrame.new(
|
720
678
|
# {
|
721
|
-
# "
|
722
|
-
#
|
723
|
-
#
|
724
|
-
#
|
725
|
-
#
|
679
|
+
# "datetime" => [
|
680
|
+
# Time.utc(1978, 1, 1, 1, 1, 1, 0),
|
681
|
+
# Time.utc(2024, 10, 13, 5, 30, 14, 500_000),
|
682
|
+
# Time.utc(2065, 1, 1, 10, 20, 30, 60_000)
|
683
|
+
# ]
|
726
684
|
# }
|
727
685
|
# )
|
728
|
-
#
|
729
|
-
#
|
730
|
-
#
|
731
|
-
#
|
732
|
-
#
|
733
|
-
# # │ datetime[μs] │
|
734
|
-
# # ╞════════════════════════════╡
|
735
|
-
# # │ 2001-01-01 00:00:00.456789 │
|
736
|
-
# # │ 2001-01-01 00:00:03.111110 │
|
737
|
-
# # │ 2001-01-01 00:00:05.765431 │
|
738
|
-
# # └────────────────────────────┘
|
739
|
-
#
|
740
|
-
# @example
|
741
|
-
# df.select(Polars.col("date").dt.second.alias("secs"))
|
742
|
-
# # =>
|
743
|
-
# # shape: (3, 1)
|
744
|
-
# # ┌──────┐
|
745
|
-
# # │ secs │
|
746
|
-
# # │ --- │
|
747
|
-
# # │ i8 │
|
748
|
-
# # ╞══════╡
|
749
|
-
# # │ 0 │
|
750
|
-
# # │ 3 │
|
751
|
-
# # │ 5 │
|
752
|
-
# # └──────┘
|
753
|
-
#
|
754
|
-
# df.select(Polars.col("date").dt.second(fractional: true).alias("secs"))
|
755
|
-
# # =>
|
756
|
-
# # shape: (3, 1)
|
757
|
-
# # ┌──────────┐
|
758
|
-
# # │ secs │
|
759
|
-
# # │ --- │
|
760
|
-
# # │ f64 │
|
761
|
-
# # ╞══════════╡
|
762
|
-
# # │ 0.456789 │
|
763
|
-
# # │ 3.11111 │
|
764
|
-
# # │ 5.765431 │
|
765
|
-
# # └──────────┘
|
766
|
-
#
|
767
|
-
# @example
|
768
|
-
# start = DateTime.new(2001, 1, 1)
|
769
|
-
# stop = DateTime.new(2001, 1, 1, 0, 0, 4)
|
770
|
-
# df = Polars::DataFrame.new(
|
771
|
-
# {"date" => Polars.date_range(start, stop, "2s")}
|
686
|
+
# df.with_columns(
|
687
|
+
# Polars.col("datetime").dt.hour.alias("hour"),
|
688
|
+
# Polars.col("datetime").dt.minute.alias("minute"),
|
689
|
+
# Polars.col("datetime").dt.second.alias("second"),
|
690
|
+
# Polars.col("datetime").dt.millisecond.alias("millisecond")
|
772
691
|
# )
|
773
692
|
# # =>
|
774
|
-
# # shape: (3,
|
775
|
-
# #
|
776
|
-
# # │
|
777
|
-
# # │ ---
|
778
|
-
# # │ datetime[
|
779
|
-
# #
|
780
|
-
# # │
|
781
|
-
# # │
|
782
|
-
# # │
|
783
|
-
# #
|
693
|
+
# # shape: (3, 5)
|
694
|
+
# # ┌─────────────────────────┬──────┬────────┬────────┬─────────────┐
|
695
|
+
# # │ datetime ┆ hour ┆ minute ┆ second ┆ millisecond │
|
696
|
+
# # │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
|
697
|
+
# # │ datetime[ns] ┆ i8 ┆ i8 ┆ i8 ┆ i32 │
|
698
|
+
# # ╞═════════════════════════╪══════╪════════╪════════╪═════════════╡
|
699
|
+
# # │ 1978-01-01 01:01:01 ┆ 1 ┆ 1 ┆ 1 ┆ 0 │
|
700
|
+
# # │ 2024-10-13 05:30:14.500 ┆ 5 ┆ 30 ┆ 14 ┆ 500 │
|
701
|
+
# # │ 2065-01-01 10:20:30.060 ┆ 10 ┆ 20 ┆ 30 ┆ 60 │
|
702
|
+
# # └─────────────────────────┴──────┴────────┴────────┴─────────────┘
|
784
703
|
#
|
785
704
|
# @example
|
786
|
-
# df.
|
705
|
+
# df.with_columns(
|
706
|
+
# Polars.col("datetime").dt.hour.alias("hour"),
|
707
|
+
# Polars.col("datetime").dt.minute.alias("minute"),
|
708
|
+
# Polars.col("datetime").dt.second(fractional: true).alias("second")
|
709
|
+
# )
|
787
710
|
# # =>
|
788
|
-
# # shape: (3,
|
789
|
-
# #
|
790
|
-
# # │
|
791
|
-
# # │ --- │
|
792
|
-
# # │ i8 │
|
793
|
-
# #
|
794
|
-
# # │ 0 │
|
795
|
-
# # │
|
796
|
-
# # │
|
797
|
-
# #
|
711
|
+
# # shape: (3, 4)
|
712
|
+
# # ┌─────────────────────────┬──────┬────────┬────────┐
|
713
|
+
# # │ datetime ┆ hour ┆ minute ┆ second │
|
714
|
+
# # │ --- ┆ --- ┆ --- ┆ --- │
|
715
|
+
# # │ datetime[ns] ┆ i8 ┆ i8 ┆ f64 │
|
716
|
+
# # ╞═════════════════════════╪══════╪════════╪════════╡
|
717
|
+
# # │ 1978-01-01 01:01:01 ┆ 1 ┆ 1 ┆ 1.0 │
|
718
|
+
# # │ 2024-10-13 05:30:14.500 ┆ 5 ┆ 30 ┆ 14.5 │
|
719
|
+
# # │ 2065-01-01 10:20:30.060 ┆ 10 ┆ 20 ┆ 30.06 │
|
720
|
+
# # └─────────────────────────┴──────┴────────┴────────┘
|
798
721
|
def second(fractional: false)
|
799
|
-
sec = Utils.wrap_expr(_rbexpr.
|
722
|
+
sec = Utils.wrap_expr(_rbexpr.dt_second)
|
800
723
|
if fractional
|
801
|
-
sec + (Utils.wrap_expr(_rbexpr.
|
724
|
+
sec + (Utils.wrap_expr(_rbexpr.dt_nanosecond) / F.lit(1_000_000_000.0))
|
802
725
|
else
|
803
726
|
sec
|
804
727
|
end
|
@@ -810,7 +733,7 @@ module Polars
|
|
810
733
|
#
|
811
734
|
# @return [Expr]
|
812
735
|
def millisecond
|
813
|
-
Utils.wrap_expr(_rbexpr.
|
736
|
+
Utils.wrap_expr(_rbexpr.dt_millisecond)
|
814
737
|
end
|
815
738
|
|
816
739
|
# Extract microseconds from underlying DateTime representation.
|
@@ -819,7 +742,7 @@ module Polars
|
|
819
742
|
#
|
820
743
|
# @return [Expr]
|
821
744
|
def microsecond
|
822
|
-
Utils.wrap_expr(_rbexpr.
|
745
|
+
Utils.wrap_expr(_rbexpr.dt_microsecond)
|
823
746
|
end
|
824
747
|
|
825
748
|
# Extract nanoseconds from underlying DateTime representation.
|
@@ -828,81 +751,79 @@ module Polars
|
|
828
751
|
#
|
829
752
|
# @return [Expr]
|
830
753
|
def nanosecond
|
831
|
-
Utils.wrap_expr(_rbexpr.
|
754
|
+
Utils.wrap_expr(_rbexpr.dt_nanosecond)
|
832
755
|
end
|
833
756
|
|
834
757
|
# Get the time passed since the Unix EPOCH in the give time unit.
|
835
758
|
#
|
836
|
-
# @param
|
759
|
+
# @param time_unit ["us", "ns", "ms", "s", "d"]
|
837
760
|
# Time unit.
|
838
761
|
#
|
839
762
|
# @return [Expr]
|
840
763
|
#
|
841
764
|
# @example
|
842
|
-
#
|
843
|
-
#
|
844
|
-
#
|
845
|
-
#
|
846
|
-
#
|
847
|
-
#
|
848
|
-
#
|
849
|
-
#
|
850
|
-
# ]
|
765
|
+
# df = (
|
766
|
+
# Polars.date_range(Date.new(2001, 1, 1), Date.new(2001, 1, 3), eager: true)
|
767
|
+
# .alias("date")
|
768
|
+
# .to_frame
|
769
|
+
# )
|
770
|
+
# df.with_columns(
|
771
|
+
# Polars.col("date").dt.epoch.alias("epoch_ns"),
|
772
|
+
# Polars.col("date").dt.epoch("s").alias("epoch_s")
|
851
773
|
# )
|
852
774
|
# # =>
|
853
775
|
# # shape: (3, 3)
|
854
|
-
# #
|
855
|
-
# # │ date
|
856
|
-
# # │ ---
|
857
|
-
# # │
|
858
|
-
# #
|
859
|
-
# # │ 2001-01-01
|
860
|
-
# # │ 2001-01-02
|
861
|
-
# # │ 2001-01-03
|
862
|
-
# #
|
863
|
-
def epoch(
|
864
|
-
if Utils::DTYPE_TEMPORAL_UNITS.include?(
|
865
|
-
timestamp(
|
866
|
-
elsif
|
776
|
+
# # ┌────────────┬─────────────────┬───────────┐
|
777
|
+
# # │ date ┆ epoch_ns ┆ epoch_s │
|
778
|
+
# # │ --- ┆ --- ┆ --- │
|
779
|
+
# # │ date ┆ i64 ┆ i64 │
|
780
|
+
# # ╞════════════╪═════════════════╪═══════════╡
|
781
|
+
# # │ 2001-01-01 ┆ 978307200000000 ┆ 978307200 │
|
782
|
+
# # │ 2001-01-02 ┆ 978393600000000 ┆ 978393600 │
|
783
|
+
# # │ 2001-01-03 ┆ 978480000000000 ┆ 978480000 │
|
784
|
+
# # └────────────┴─────────────────┴───────────┘
|
785
|
+
def epoch(time_unit = "us")
|
786
|
+
if Utils::DTYPE_TEMPORAL_UNITS.include?(time_unit)
|
787
|
+
timestamp(time_unit)
|
788
|
+
elsif time_unit == "s"
|
867
789
|
Utils.wrap_expr(_rbexpr.dt_epoch_seconds)
|
868
|
-
elsif
|
790
|
+
elsif time_unit == "d"
|
869
791
|
Utils.wrap_expr(_rbexpr).cast(:date).cast(:i32)
|
870
792
|
else
|
871
|
-
raise ArgumentError, "
|
793
|
+
raise ArgumentError, "time_unit must be one of {'ns', 'us', 'ms', 's', 'd'}, got #{time_unit.inspect}"
|
872
794
|
end
|
873
795
|
end
|
874
796
|
|
875
797
|
# Return a timestamp in the given time unit.
|
876
798
|
#
|
877
|
-
# @param
|
799
|
+
# @param time_unit ["us", "ns", "ms"]
|
878
800
|
# Time unit.
|
879
801
|
#
|
880
802
|
# @return [Expr]
|
881
803
|
#
|
882
804
|
# @example
|
883
|
-
#
|
884
|
-
#
|
885
|
-
#
|
886
|
-
#
|
887
|
-
#
|
888
|
-
#
|
889
|
-
#
|
890
|
-
#
|
891
|
-
# ]
|
805
|
+
# df = (
|
806
|
+
# Polars.date_range(Date.new(2001, 1, 1), Date.new(2001, 1, 3), eager: true)
|
807
|
+
# .alias("date")
|
808
|
+
# .to_frame
|
809
|
+
# )
|
810
|
+
# df.with_columns(
|
811
|
+
# Polars.col("date").dt.timestamp.alias("timestamp_us"),
|
812
|
+
# Polars.col("date").dt.timestamp("ms").alias("timestamp_ms")
|
892
813
|
# )
|
893
814
|
# # =>
|
894
815
|
# # shape: (3, 3)
|
895
|
-
# #
|
896
|
-
# # │ date
|
897
|
-
# # │ ---
|
898
|
-
# # │
|
899
|
-
# #
|
900
|
-
# # │ 2001-01-01
|
901
|
-
# # │ 2001-01-02
|
902
|
-
# # │ 2001-01-03
|
903
|
-
# #
|
904
|
-
def timestamp(
|
905
|
-
Utils.wrap_expr(_rbexpr.
|
816
|
+
# # ┌────────────┬─────────────────┬──────────────┐
|
817
|
+
# # │ date ┆ timestamp_us ┆ timestamp_ms │
|
818
|
+
# # │ --- ┆ --- ┆ --- │
|
819
|
+
# # │ date ┆ i64 ┆ i64 │
|
820
|
+
# # ╞════════════╪═════════════════╪══════════════╡
|
821
|
+
# # │ 2001-01-01 ┆ 978307200000000 ┆ 978307200000 │
|
822
|
+
# # │ 2001-01-02 ┆ 978393600000000 ┆ 978393600000 │
|
823
|
+
# # │ 2001-01-03 ┆ 978480000000000 ┆ 978480000000 │
|
824
|
+
# # └────────────┴─────────────────┴──────────────┘
|
825
|
+
def timestamp(time_unit = "us")
|
826
|
+
Utils.wrap_expr(_rbexpr.dt_timestamp(time_unit))
|
906
827
|
end
|
907
828
|
|
908
829
|
# Set time unit of a Series of dtype Datetime or Duration.
|
@@ -910,43 +831,17 @@ module Polars
|
|
910
831
|
# This does not modify underlying data, and should be used to fix an incorrect
|
911
832
|
# time unit.
|
912
833
|
#
|
913
|
-
# @param
|
834
|
+
# @param time_unit ["ns", "us", "ms"]
|
914
835
|
# Time unit for the `Datetime` Series.
|
915
836
|
#
|
916
837
|
# @return [Expr]
|
917
|
-
|
918
|
-
|
919
|
-
# df = Polars::DataFrame.new(
|
920
|
-
# {
|
921
|
-
# "date" => Polars.date_range(
|
922
|
-
# DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 3), "1d", time_unit: "ns"
|
923
|
-
# )
|
924
|
-
# }
|
925
|
-
# )
|
926
|
-
# df.select(
|
927
|
-
# [
|
928
|
-
# Polars.col("date"),
|
929
|
-
# Polars.col("date").dt.with_time_unit("us").alias("tu_us")
|
930
|
-
# ]
|
931
|
-
# )
|
932
|
-
# # =>
|
933
|
-
# # shape: (3, 2)
|
934
|
-
# # ┌─────────────────────┬───────────────────────┐
|
935
|
-
# # │ date ┆ tu_us │
|
936
|
-
# # │ --- ┆ --- │
|
937
|
-
# # │ datetime[ns] ┆ datetime[μs] │
|
938
|
-
# # ╞═════════════════════╪═══════════════════════╡
|
939
|
-
# # │ 2001-01-01 00:00:00 ┆ +32971-04-28 00:00:00 │
|
940
|
-
# # │ 2001-01-02 00:00:00 ┆ +32974-01-22 00:00:00 │
|
941
|
-
# # │ 2001-01-03 00:00:00 ┆ +32976-10-18 00:00:00 │
|
942
|
-
# # └─────────────────────┴───────────────────────┘
|
943
|
-
def with_time_unit(tu)
|
944
|
-
Utils.wrap_expr(_rbexpr.dt_with_time_unit(tu))
|
838
|
+
def with_time_unit(time_unit)
|
839
|
+
Utils.wrap_expr(_rbexpr.dt_with_time_unit(time_unit))
|
945
840
|
end
|
946
841
|
|
947
842
|
# Cast the underlying data to another time unit. This may lose precision.
|
948
843
|
#
|
949
|
-
# @param
|
844
|
+
# @param time_unit ["ns", "us", "ms"]
|
950
845
|
# Time unit for the `Datetime` Series.
|
951
846
|
#
|
952
847
|
# @return [Expr]
|
@@ -954,8 +849,8 @@ module Polars
|
|
954
849
|
# @example
|
955
850
|
# df = Polars::DataFrame.new(
|
956
851
|
# {
|
957
|
-
# "date" => Polars.
|
958
|
-
# DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 3), "1d"
|
852
|
+
# "date" => Polars.datetime_range(
|
853
|
+
# DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 3), "1d", eager: true
|
959
854
|
# )
|
960
855
|
# }
|
961
856
|
# )
|
@@ -971,19 +866,19 @@ module Polars
|
|
971
866
|
# # ┌─────────────────────┬─────────────────────┬─────────────────────┐
|
972
867
|
# # │ date ┆ tu_ms ┆ tu_ns │
|
973
868
|
# # │ --- ┆ --- ┆ --- │
|
974
|
-
# # │ datetime[
|
869
|
+
# # │ datetime[ns] ┆ datetime[ms] ┆ datetime[ns] │
|
975
870
|
# # ╞═════════════════════╪═════════════════════╪═════════════════════╡
|
976
871
|
# # │ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 ┆ 2001-01-01 00:00:00 │
|
977
872
|
# # │ 2001-01-02 00:00:00 ┆ 2001-01-02 00:00:00 ┆ 2001-01-02 00:00:00 │
|
978
873
|
# # │ 2001-01-03 00:00:00 ┆ 2001-01-03 00:00:00 ┆ 2001-01-03 00:00:00 │
|
979
874
|
# # └─────────────────────┴─────────────────────┴─────────────────────┘
|
980
|
-
def cast_time_unit(
|
981
|
-
Utils.wrap_expr(_rbexpr.dt_cast_time_unit(
|
875
|
+
def cast_time_unit(time_unit)
|
876
|
+
Utils.wrap_expr(_rbexpr.dt_cast_time_unit(time_unit))
|
982
877
|
end
|
983
878
|
|
984
879
|
# Set time zone for a Series of type Datetime.
|
985
880
|
#
|
986
|
-
# @param
|
881
|
+
# @param time_zone [String]
|
987
882
|
# Time zone for the `Datetime` Series.
|
988
883
|
#
|
989
884
|
# @return [Expr]
|
@@ -991,11 +886,12 @@ module Polars
|
|
991
886
|
# @example
|
992
887
|
# df = Polars::DataFrame.new(
|
993
888
|
# {
|
994
|
-
# "date" => Polars.
|
889
|
+
# "date" => Polars.datetime_range(
|
995
890
|
# DateTime.new(2020, 3, 1),
|
996
891
|
# DateTime.new(2020, 5, 1),
|
997
892
|
# "1mo",
|
998
|
-
# time_zone: "UTC"
|
893
|
+
# time_zone: "UTC",
|
894
|
+
# eager: true
|
999
895
|
# )
|
1000
896
|
# }
|
1001
897
|
# )
|
@@ -1012,14 +908,14 @@ module Polars
|
|
1012
908
|
# # ┌─────────────────────────┬─────────────────────────────┐
|
1013
909
|
# # │ date ┆ London │
|
1014
910
|
# # │ --- ┆ --- │
|
1015
|
-
# # │ datetime[
|
911
|
+
# # │ datetime[ns, UTC] ┆ datetime[ns, Europe/London] │
|
1016
912
|
# # ╞═════════════════════════╪═════════════════════════════╡
|
1017
913
|
# # │ 2020-03-01 00:00:00 UTC ┆ 2020-03-01 00:00:00 GMT │
|
1018
914
|
# # │ 2020-04-01 00:00:00 UTC ┆ 2020-04-01 01:00:00 BST │
|
1019
915
|
# # │ 2020-05-01 00:00:00 UTC ┆ 2020-05-01 01:00:00 BST │
|
1020
916
|
# # └─────────────────────────┴─────────────────────────────┘
|
1021
|
-
def convert_time_zone(
|
1022
|
-
Utils.wrap_expr(_rbexpr.dt_convert_time_zone(
|
917
|
+
def convert_time_zone(time_zone)
|
918
|
+
Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone))
|
1023
919
|
end
|
1024
920
|
|
1025
921
|
# Cast time zone for a Series of type Datetime.
|
@@ -1029,17 +925,16 @@ module Polars
|
|
1029
925
|
#
|
1030
926
|
# @param time_zone [String]
|
1031
927
|
# Time zone for the `Datetime` Series. Pass `nil` to unset time zone.
|
1032
|
-
# @param use_earliest [Boolean]
|
1033
|
-
# Determine how to deal with ambiguous datetimes.
|
1034
928
|
# @param ambiguous [String]
|
1035
929
|
# Determine how to deal with ambiguous datetimes.
|
1036
930
|
# @param non_existent [String]
|
1037
931
|
# Determine how to deal with non-existent datetimes.
|
1038
932
|
#
|
1039
933
|
# @return [Expr]
|
1040
|
-
def replace_time_zone(time_zone,
|
1041
|
-
ambiguous
|
1042
|
-
|
934
|
+
def replace_time_zone(time_zone, ambiguous: "raise", non_existent: "raise")
|
935
|
+
unless ambiguous.is_a?(Expr)
|
936
|
+
ambiguous = Polars.lit(ambiguous)
|
937
|
+
end
|
1043
938
|
Utils.wrap_expr(_rbexpr.dt_replace_time_zone(time_zone, ambiguous._rbexpr, non_existent))
|
1044
939
|
end
|
1045
940
|
|
@@ -1050,8 +945,8 @@ module Polars
|
|
1050
945
|
# @example
|
1051
946
|
# df = Polars::DataFrame.new(
|
1052
947
|
# {
|
1053
|
-
# "date" => Polars.
|
1054
|
-
# DateTime.new(2020, 3, 1), DateTime.new(2020, 5, 1), "1mo"
|
948
|
+
# "date" => Polars.datetime_range(
|
949
|
+
# DateTime.new(2020, 3, 1), DateTime.new(2020, 5, 1), "1mo", eager: true
|
1055
950
|
# )
|
1056
951
|
# }
|
1057
952
|
# )
|
@@ -1066,7 +961,7 @@ module Polars
|
|
1066
961
|
# # ┌─────────────────────┬───────────┐
|
1067
962
|
# # │ date ┆ days_diff │
|
1068
963
|
# # │ --- ┆ --- │
|
1069
|
-
# # │ datetime[
|
964
|
+
# # │ datetime[ns] ┆ i64 │
|
1070
965
|
# # ╞═════════════════════╪═══════════╡
|
1071
966
|
# # │ 2020-03-01 00:00:00 ┆ null │
|
1072
967
|
# # │ 2020-04-01 00:00:00 ┆ 31 │
|
@@ -1084,8 +979,8 @@ module Polars
|
|
1084
979
|
# @example
|
1085
980
|
# df = Polars::DataFrame.new(
|
1086
981
|
# {
|
1087
|
-
# "date" => Polars.
|
1088
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 4), "1d"
|
982
|
+
# "date" => Polars.datetime_range(
|
983
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 4), "1d", eager: true
|
1089
984
|
# )
|
1090
985
|
# }
|
1091
986
|
# )
|
@@ -1100,7 +995,7 @@ module Polars
|
|
1100
995
|
# # ┌─────────────────────┬────────────┐
|
1101
996
|
# # │ date ┆ hours_diff │
|
1102
997
|
# # │ --- ┆ --- │
|
1103
|
-
# # │ datetime[
|
998
|
+
# # │ datetime[ns] ┆ i64 │
|
1104
999
|
# # ╞═════════════════════╪════════════╡
|
1105
1000
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1106
1001
|
# # │ 2020-01-02 00:00:00 ┆ 24 │
|
@@ -1119,8 +1014,8 @@ module Polars
|
|
1119
1014
|
# @example
|
1120
1015
|
# df = Polars::DataFrame.new(
|
1121
1016
|
# {
|
1122
|
-
# "date" => Polars.
|
1123
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 4), "1d"
|
1017
|
+
# "date" => Polars.datetime_range(
|
1018
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 4), "1d", eager: true
|
1124
1019
|
# )
|
1125
1020
|
# }
|
1126
1021
|
# )
|
@@ -1135,7 +1030,7 @@ module Polars
|
|
1135
1030
|
# # ┌─────────────────────┬──────────────┐
|
1136
1031
|
# # │ date ┆ minutes_diff │
|
1137
1032
|
# # │ --- ┆ --- │
|
1138
|
-
# # │ datetime[
|
1033
|
+
# # │ datetime[ns] ┆ i64 │
|
1139
1034
|
# # ╞═════════════════════╪══════════════╡
|
1140
1035
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1141
1036
|
# # │ 2020-01-02 00:00:00 ┆ 1440 │
|
@@ -1154,8 +1049,8 @@ module Polars
|
|
1154
1049
|
# @example
|
1155
1050
|
# df = Polars::DataFrame.new(
|
1156
1051
|
# {
|
1157
|
-
# "date" => Polars.
|
1158
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 4, 0), "1m"
|
1052
|
+
# "date" => Polars.datetime_range(
|
1053
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 4, 0), "1m", eager: true
|
1159
1054
|
# )
|
1160
1055
|
# }
|
1161
1056
|
# )
|
@@ -1170,7 +1065,7 @@ module Polars
|
|
1170
1065
|
# # ┌─────────────────────┬──────────────┐
|
1171
1066
|
# # │ date ┆ seconds_diff │
|
1172
1067
|
# # │ --- ┆ --- │
|
1173
|
-
# # │ datetime[
|
1068
|
+
# # │ datetime[ns] ┆ i64 │
|
1174
1069
|
# # ╞═════════════════════╪══════════════╡
|
1175
1070
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1176
1071
|
# # │ 2020-01-01 00:01:00 ┆ 60 │
|
@@ -1190,8 +1085,8 @@ module Polars
|
|
1190
1085
|
# @example
|
1191
1086
|
# df = Polars::DataFrame.new(
|
1192
1087
|
# {
|
1193
|
-
# "date" => Polars.
|
1194
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms"
|
1088
|
+
# "date" => Polars.datetime_range(
|
1089
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms", eager: true
|
1195
1090
|
# )
|
1196
1091
|
# }
|
1197
1092
|
# )
|
@@ -1206,7 +1101,7 @@ module Polars
|
|
1206
1101
|
# # ┌─────────────────────────┬───────────────────┐
|
1207
1102
|
# # │ date ┆ milliseconds_diff │
|
1208
1103
|
# # │ --- ┆ --- │
|
1209
|
-
# # │ datetime[
|
1104
|
+
# # │ datetime[ns] ┆ i64 │
|
1210
1105
|
# # ╞═════════════════════════╪═══════════════════╡
|
1211
1106
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1212
1107
|
# # │ 2020-01-01 00:00:00.001 ┆ 1 │
|
@@ -1232,8 +1127,8 @@ module Polars
|
|
1232
1127
|
# @example
|
1233
1128
|
# df = Polars::DataFrame.new(
|
1234
1129
|
# {
|
1235
|
-
# "date" => Polars.
|
1236
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms"
|
1130
|
+
# "date" => Polars.datetime_range(
|
1131
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms", eager: true
|
1237
1132
|
# )
|
1238
1133
|
# }
|
1239
1134
|
# )
|
@@ -1248,7 +1143,7 @@ module Polars
|
|
1248
1143
|
# # ┌─────────────────────────┬───────────────────┐
|
1249
1144
|
# # │ date ┆ microseconds_diff │
|
1250
1145
|
# # │ --- ┆ --- │
|
1251
|
-
# # │ datetime[
|
1146
|
+
# # │ datetime[ns] ┆ i64 │
|
1252
1147
|
# # ╞═════════════════════════╪═══════════════════╡
|
1253
1148
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1254
1149
|
# # │ 2020-01-01 00:00:00.001 ┆ 1000 │
|
@@ -1274,8 +1169,8 @@ module Polars
|
|
1274
1169
|
# @example
|
1275
1170
|
# df = Polars::DataFrame.new(
|
1276
1171
|
# {
|
1277
|
-
# "date" => Polars.
|
1278
|
-
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms"
|
1172
|
+
# "date" => Polars.datetime_range(
|
1173
|
+
# DateTime.new(2020, 1, 1), DateTime.new(2020, 1, 1, 0, 0, 1), "1ms", eager: true
|
1279
1174
|
# )
|
1280
1175
|
# }
|
1281
1176
|
# )
|
@@ -1290,7 +1185,7 @@ module Polars
|
|
1290
1185
|
# # ┌─────────────────────────┬──────────────────┐
|
1291
1186
|
# # │ date ┆ nanoseconds_diff │
|
1292
1187
|
# # │ --- ┆ --- │
|
1293
|
-
# # │ datetime[
|
1188
|
+
# # │ datetime[ns] ┆ i64 │
|
1294
1189
|
# # ╞═════════════════════════╪══════════════════╡
|
1295
1190
|
# # │ 2020-01-01 00:00:00 ┆ null │
|
1296
1191
|
# # │ 2020-01-01 00:00:00.001 ┆ 1000000 │
|
@@ -1335,8 +1230,8 @@ module Polars
|
|
1335
1230
|
# @example
|
1336
1231
|
# df = Polars::DataFrame.new(
|
1337
1232
|
# {
|
1338
|
-
# "dates" => Polars.
|
1339
|
-
# DateTime.new(2000, 1, 1), DateTime.new(2005, 1, 1), "1y"
|
1233
|
+
# "dates" => Polars.datetime_range(
|
1234
|
+
# DateTime.new(2000, 1, 1), DateTime.new(2005, 1, 1), "1y", eager: true
|
1340
1235
|
# )
|
1341
1236
|
# }
|
1342
1237
|
# )
|
@@ -1351,7 +1246,7 @@ module Polars
|
|
1351
1246
|
# # ┌─────────────────────┬─────────────────────┐
|
1352
1247
|
# # │ date_plus_1y ┆ date_min │
|
1353
1248
|
# # │ --- ┆ --- │
|
1354
|
-
# # │ datetime[
|
1249
|
+
# # │ datetime[ns] ┆ datetime[ns] │
|
1355
1250
|
# # ╞═════════════════════╪═════════════════════╡
|
1356
1251
|
# # │ 2001-01-01 00:00:00 ┆ 1998-11-01 00:00:00 │
|
1357
1252
|
# # │ 2002-01-01 00:00:00 ┆ 1999-11-01 00:00:00 │
|
@@ -1361,7 +1256,7 @@ module Polars
|
|
1361
1256
|
# # │ 2006-01-01 00:00:00 ┆ 2003-11-01 00:00:00 │
|
1362
1257
|
# # └─────────────────────┴─────────────────────┘
|
1363
1258
|
def offset_by(by)
|
1364
|
-
by = Utils.
|
1259
|
+
by = Utils.parse_into_expression(by, str_as_lit: true)
|
1365
1260
|
Utils.wrap_expr(_rbexpr.dt_offset_by(by))
|
1366
1261
|
end
|
1367
1262
|
|
@@ -1372,10 +1267,11 @@ module Polars
|
|
1372
1267
|
# @example
|
1373
1268
|
# df = Polars::DataFrame.new(
|
1374
1269
|
# {
|
1375
|
-
# "dates" => Polars.
|
1270
|
+
# "dates" => Polars.datetime_range(
|
1376
1271
|
# DateTime.new(2000, 1, 15, 2),
|
1377
1272
|
# DateTime.new(2000, 12, 15, 2),
|
1378
|
-
# "1mo"
|
1273
|
+
# "1mo",
|
1274
|
+
# eager: true
|
1379
1275
|
# )
|
1380
1276
|
# }
|
1381
1277
|
# )
|
@@ -1385,7 +1281,7 @@ module Polars
|
|
1385
1281
|
# # ┌─────────────────────┐
|
1386
1282
|
# # │ dates │
|
1387
1283
|
# # │ --- │
|
1388
|
-
# # │ datetime[
|
1284
|
+
# # │ datetime[ns] │
|
1389
1285
|
# # ╞═════════════════════╡
|
1390
1286
|
# # │ 2000-01-01 02:00:00 │
|
1391
1287
|
# # │ 2000-02-01 02:00:00 │
|
@@ -1410,10 +1306,11 @@ module Polars
|
|
1410
1306
|
# @example
|
1411
1307
|
# df = Polars::DataFrame.new(
|
1412
1308
|
# {
|
1413
|
-
# "dates" => Polars.
|
1309
|
+
# "dates" => Polars.datetime_range(
|
1414
1310
|
# DateTime.new(2000, 1, 15, 2),
|
1415
1311
|
# DateTime.new(2000, 12, 15, 2),
|
1416
|
-
# "1mo"
|
1312
|
+
# "1mo",
|
1313
|
+
# eager: true
|
1417
1314
|
# )
|
1418
1315
|
# }
|
1419
1316
|
# )
|
@@ -1423,7 +1320,7 @@ module Polars
|
|
1423
1320
|
# # ┌─────────────────────┐
|
1424
1321
|
# # │ dates │
|
1425
1322
|
# # │ --- │
|
1426
|
-
# # │ datetime[
|
1323
|
+
# # │ datetime[ns] │
|
1427
1324
|
# # ╞═════════════════════╡
|
1428
1325
|
# # │ 2000-01-31 02:00:00 │
|
1429
1326
|
# # │ 2000-02-29 02:00:00 │
|
@@ -1440,5 +1337,61 @@ module Polars
|
|
1440
1337
|
def month_end
|
1441
1338
|
Utils.wrap_expr(_rbexpr.dt_month_end)
|
1442
1339
|
end
|
1340
|
+
|
1341
|
+
# Base offset from UTC.
|
1342
|
+
#
|
1343
|
+
# This is usually constant for all datetimes in a given time zone, but
|
1344
|
+
# may vary in the rare case that a country switches time zone, like
|
1345
|
+
# Samoa (Apia) did at the end of 2011.
|
1346
|
+
#
|
1347
|
+
# @return [Expr]
|
1348
|
+
#
|
1349
|
+
# @example
|
1350
|
+
# df = Polars::DataFrame.new(
|
1351
|
+
# {
|
1352
|
+
# "ts" => [DateTime.new(2011, 12, 29), DateTime.new(2012, 1, 1)],
|
1353
|
+
# }
|
1354
|
+
# )
|
1355
|
+
# df = df.with_columns(Polars.col("ts").dt.replace_time_zone("Pacific/Apia"))
|
1356
|
+
# df.with_columns(Polars.col("ts").dt.base_utc_offset.alias("base_utc_offset"))
|
1357
|
+
# # =>
|
1358
|
+
# # shape: (2, 2)
|
1359
|
+
# # ┌────────────────────────────┬─────────────────┐
|
1360
|
+
# # │ ts ┆ base_utc_offset │
|
1361
|
+
# # │ --- ┆ --- │
|
1362
|
+
# # │ datetime[ns, Pacific/Apia] ┆ duration[ms] │
|
1363
|
+
# # ╞════════════════════════════╪═════════════════╡
|
1364
|
+
# # │ 2011-12-29 00:00:00 -10 ┆ -11h │
|
1365
|
+
# # │ 2012-01-01 00:00:00 +14 ┆ 13h │
|
1366
|
+
# # └────────────────────────────┴─────────────────┘
|
1367
|
+
def base_utc_offset
|
1368
|
+
Utils.wrap_expr(_rbexpr.dt_base_utc_offset)
|
1369
|
+
end
|
1370
|
+
|
1371
|
+
# Additional offset currently in effect (typically due to daylight saving time).
|
1372
|
+
#
|
1373
|
+
# @return [Expr]
|
1374
|
+
#
|
1375
|
+
# @example
|
1376
|
+
# df = Polars::DataFrame.new(
|
1377
|
+
# {
|
1378
|
+
# "ts" => [DateTime.new(2020, 10, 25), DateTime.new(2020, 10, 26)],
|
1379
|
+
# }
|
1380
|
+
# )
|
1381
|
+
# df = df.with_columns(Polars.col("ts").dt.replace_time_zone("Europe/London"))
|
1382
|
+
# df.with_columns(Polars.col("ts").dt.dst_offset.alias("dst_offset"))
|
1383
|
+
# # =>
|
1384
|
+
# # shape: (2, 2)
|
1385
|
+
# # ┌─────────────────────────────┬──────────────┐
|
1386
|
+
# # │ ts ┆ dst_offset │
|
1387
|
+
# # │ --- ┆ --- │
|
1388
|
+
# # │ datetime[ns, Europe/London] ┆ duration[ms] │
|
1389
|
+
# # ╞═════════════════════════════╪══════════════╡
|
1390
|
+
# # │ 2020-10-25 00:00:00 BST ┆ 1h │
|
1391
|
+
# # │ 2020-10-26 00:00:00 GMT ┆ 0ms │
|
1392
|
+
# # └─────────────────────────────┴──────────────┘
|
1393
|
+
def dst_offset
|
1394
|
+
Utils.wrap_expr(_rbexpr.dt_dst_offset)
|
1395
|
+
end
|
1443
1396
|
end
|
1444
1397
|
end
|