polars-df 0.15.0 → 0.26.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.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +274 -0
  3. data/Cargo.lock +1465 -867
  4. data/Cargo.toml +3 -0
  5. data/LICENSE.txt +2 -2
  6. data/README.md +87 -37
  7. data/ext/polars/Cargo.toml +47 -16
  8. data/ext/polars/src/c_api/allocator.rs +7 -0
  9. data/ext/polars/src/c_api/mod.rs +1 -0
  10. data/ext/polars/src/catalog/mod.rs +1 -0
  11. data/ext/polars/src/catalog/unity.rs +470 -0
  12. data/ext/polars/src/conversion/any_value.rs +99 -84
  13. data/ext/polars/src/conversion/categorical.rs +30 -0
  14. data/ext/polars/src/conversion/chunked_array.rs +71 -62
  15. data/ext/polars/src/conversion/datetime.rs +63 -0
  16. data/ext/polars/src/conversion/mod.rs +796 -312
  17. data/ext/polars/src/dataframe/construction.rs +6 -18
  18. data/ext/polars/src/dataframe/export.rs +30 -39
  19. data/ext/polars/src/dataframe/general.rs +294 -362
  20. data/ext/polars/src/dataframe/io.rs +33 -150
  21. data/ext/polars/src/dataframe/map.rs +175 -0
  22. data/ext/polars/src/dataframe/mod.rs +37 -5
  23. data/ext/polars/src/dataframe/serde.rs +23 -8
  24. data/ext/polars/src/error.rs +44 -7
  25. data/ext/polars/src/exceptions.rs +21 -8
  26. data/ext/polars/src/expr/array.rs +86 -22
  27. data/ext/polars/src/expr/binary.rs +50 -1
  28. data/ext/polars/src/expr/bitwise.rs +39 -0
  29. data/ext/polars/src/expr/categorical.rs +20 -0
  30. data/ext/polars/src/expr/datatype.rs +51 -0
  31. data/ext/polars/src/expr/datetime.rs +99 -41
  32. data/ext/polars/src/expr/extension.rs +12 -0
  33. data/ext/polars/src/expr/general.rs +252 -128
  34. data/ext/polars/src/expr/list.rs +56 -60
  35. data/ext/polars/src/expr/meta.rs +30 -35
  36. data/ext/polars/src/expr/mod.rs +28 -6
  37. data/ext/polars/src/expr/name.rs +29 -14
  38. data/ext/polars/src/expr/rolling.rs +111 -3
  39. data/ext/polars/src/expr/selector.rs +219 -0
  40. data/ext/polars/src/expr/serde.rs +28 -0
  41. data/ext/polars/src/expr/string.rs +118 -20
  42. data/ext/polars/src/expr/struct.rs +14 -1
  43. data/ext/polars/src/file.rs +194 -86
  44. data/ext/polars/src/functions/aggregation.rs +13 -12
  45. data/ext/polars/src/functions/business.rs +2 -3
  46. data/ext/polars/src/functions/eager.rs +3 -2
  47. data/ext/polars/src/functions/io.rs +90 -18
  48. data/ext/polars/src/functions/lazy.rs +267 -118
  49. data/ext/polars/src/functions/meta.rs +8 -7
  50. data/ext/polars/src/functions/misc.rs +1 -1
  51. data/ext/polars/src/functions/mod.rs +2 -1
  52. data/ext/polars/src/functions/range.rs +88 -31
  53. data/ext/polars/src/functions/strings.rs +6 -0
  54. data/ext/polars/src/functions/utils.rs +8 -0
  55. data/ext/polars/src/interop/arrow/mod.rs +52 -1
  56. data/ext/polars/src/interop/arrow/{to_ruby.rs → to_rb.rs} +37 -7
  57. data/ext/polars/src/interop/arrow/to_rust.rs +43 -0
  58. data/ext/polars/src/interop/numo/to_numo_df.rs +1 -1
  59. data/ext/polars/src/interop/numo/to_numo_series.rs +72 -50
  60. data/ext/polars/src/io/cloud_options.rs +107 -0
  61. data/ext/polars/src/io/mod.rs +4 -0
  62. data/ext/polars/src/io/scan_options.rs +113 -0
  63. data/ext/polars/src/io/sink_options.rs +46 -0
  64. data/ext/polars/src/io/sink_output.rs +21 -0
  65. data/ext/polars/src/lazyframe/exitable.rs +39 -0
  66. data/ext/polars/src/lazyframe/general.rs +846 -368
  67. data/ext/polars/src/lazyframe/mod.rs +58 -5
  68. data/ext/polars/src/lazyframe/optflags.rs +59 -0
  69. data/ext/polars/src/lazyframe/serde.rs +36 -4
  70. data/ext/polars/src/lazyframe/sink.rs +46 -0
  71. data/ext/polars/src/lazygroupby.rs +38 -9
  72. data/ext/polars/src/lib.rs +574 -165
  73. data/ext/polars/src/map/lazy.rs +44 -74
  74. data/ext/polars/src/map/mod.rs +18 -254
  75. data/ext/polars/src/map/series.rs +241 -1087
  76. data/ext/polars/src/on_startup.rs +192 -9
  77. data/ext/polars/src/prelude.rs +1 -0
  78. data/ext/polars/src/rb_modules.rs +10 -57
  79. data/ext/polars/src/ruby/exceptions.rs +26 -0
  80. data/ext/polars/src/ruby/gvl.rs +104 -0
  81. data/ext/polars/src/ruby/lazy.rs +46 -0
  82. data/ext/polars/src/ruby/mod.rs +11 -0
  83. data/ext/polars/src/ruby/numo.rs +52 -0
  84. data/ext/polars/src/ruby/plan_callback.rs +198 -0
  85. data/ext/polars/src/ruby/rb_modules.rs +16 -0
  86. data/ext/polars/src/ruby/ruby_convert_registry.rs +51 -0
  87. data/ext/polars/src/ruby/ruby_function.rs +11 -0
  88. data/ext/polars/src/ruby/ruby_udf.rs +164 -0
  89. data/ext/polars/src/ruby/thread.rs +65 -0
  90. data/ext/polars/src/ruby/utils.rs +39 -0
  91. data/ext/polars/src/series/aggregation.rs +116 -91
  92. data/ext/polars/src/series/arithmetic.rs +16 -22
  93. data/ext/polars/src/series/comparison.rs +101 -222
  94. data/ext/polars/src/series/construction.rs +80 -70
  95. data/ext/polars/src/series/export.rs +98 -56
  96. data/ext/polars/src/series/general.rs +323 -440
  97. data/ext/polars/src/series/import.rs +22 -5
  98. data/ext/polars/src/series/map.rs +103 -0
  99. data/ext/polars/src/series/mod.rs +57 -15
  100. data/ext/polars/src/series/scatter.rs +139 -82
  101. data/ext/polars/src/sql.rs +16 -9
  102. data/ext/polars/src/testing/frame.rs +31 -0
  103. data/ext/polars/src/testing/mod.rs +5 -0
  104. data/ext/polars/src/testing/series.rs +31 -0
  105. data/ext/polars/src/timeout.rs +105 -0
  106. data/ext/polars/src/utils.rs +105 -4
  107. data/lib/polars/array_expr.rb +500 -22
  108. data/lib/polars/array_name_space.rb +384 -10
  109. data/lib/polars/batched_csv_reader.rb +48 -66
  110. data/lib/polars/binary_expr.rb +217 -0
  111. data/lib/polars/binary_name_space.rb +155 -1
  112. data/lib/polars/cat_expr.rb +224 -0
  113. data/lib/polars/cat_name_space.rb +132 -32
  114. data/lib/polars/catalog/unity/catalog_info.rb +20 -0
  115. data/lib/polars/catalog/unity/column_info.rb +31 -0
  116. data/lib/polars/catalog/unity/namespace_info.rb +21 -0
  117. data/lib/polars/catalog/unity/table_info.rb +50 -0
  118. data/lib/polars/catalog.rb +448 -0
  119. data/lib/polars/collect_batches.rb +22 -0
  120. data/lib/polars/config.rb +3 -3
  121. data/lib/polars/convert.rb +201 -36
  122. data/lib/polars/data_frame.rb +2851 -1017
  123. data/lib/polars/data_frame_plot.rb +173 -0
  124. data/lib/polars/data_type_expr.rb +52 -0
  125. data/lib/polars/data_type_group.rb +6 -0
  126. data/lib/polars/data_types.rb +118 -18
  127. data/lib/polars/date_time_expr.rb +426 -84
  128. data/lib/polars/date_time_name_space.rb +384 -111
  129. data/lib/polars/dynamic_group_by.rb +102 -10
  130. data/lib/polars/exceptions.rb +50 -5
  131. data/lib/polars/expr.rb +2159 -915
  132. data/lib/polars/extension_expr.rb +39 -0
  133. data/lib/polars/extension_name_space.rb +39 -0
  134. data/lib/polars/functions/aggregation/horizontal.rb +11 -6
  135. data/lib/polars/functions/aggregation/vertical.rb +2 -3
  136. data/lib/polars/functions/as_datatype.rb +290 -8
  137. data/lib/polars/functions/business.rb +95 -0
  138. data/lib/polars/functions/col.rb +6 -5
  139. data/lib/polars/functions/datatype.rb +62 -0
  140. data/lib/polars/functions/eager.rb +426 -24
  141. data/lib/polars/functions/escape_regex.rb +21 -0
  142. data/lib/polars/functions/lazy.rb +813 -195
  143. data/lib/polars/functions/lit.rb +21 -10
  144. data/lib/polars/functions/range/int_range.rb +74 -2
  145. data/lib/polars/functions/range/linear_space.rb +195 -0
  146. data/lib/polars/functions/range/time_range.rb +1 -1
  147. data/lib/polars/functions/repeat.rb +7 -12
  148. data/lib/polars/functions/whenthen.rb +2 -2
  149. data/lib/polars/group_by.rb +188 -58
  150. data/lib/polars/iceberg_dataset.rb +108 -0
  151. data/lib/polars/in_process_query.rb +37 -0
  152. data/lib/polars/io/cloud.rb +18 -0
  153. data/lib/polars/io/csv.rb +336 -128
  154. data/lib/polars/io/database.rb +19 -4
  155. data/lib/polars/io/delta.rb +134 -0
  156. data/lib/polars/io/iceberg.rb +34 -0
  157. data/lib/polars/io/ipc.rb +63 -63
  158. data/lib/polars/io/json.rb +16 -0
  159. data/lib/polars/io/lines.rb +172 -0
  160. data/lib/polars/io/ndjson.rb +176 -20
  161. data/lib/polars/io/parquet.rb +173 -95
  162. data/lib/polars/io/scan_options.rb +55 -0
  163. data/lib/polars/io/sink_options.rb +27 -0
  164. data/lib/polars/io/utils.rb +17 -0
  165. data/lib/polars/lazy_frame.rb +3017 -622
  166. data/lib/polars/lazy_group_by.rb +436 -2
  167. data/lib/polars/list_expr.rb +551 -59
  168. data/lib/polars/list_name_space.rb +465 -51
  169. data/lib/polars/meta_expr.rb +146 -24
  170. data/lib/polars/name_expr.rb +87 -2
  171. data/lib/polars/query_opt_flags.rb +264 -0
  172. data/lib/polars/rolling_group_by.rb +90 -5
  173. data/lib/polars/scan_cast_options.rb +86 -0
  174. data/lib/polars/schema.rb +128 -0
  175. data/lib/polars/selector.rb +245 -0
  176. data/lib/polars/selectors.rb +1048 -201
  177. data/lib/polars/series.rb +2522 -774
  178. data/lib/polars/series_plot.rb +72 -0
  179. data/lib/polars/slice.rb +1 -1
  180. data/lib/polars/sql_context.rb +13 -6
  181. data/lib/polars/string_cache.rb +19 -72
  182. data/lib/polars/string_expr.rb +561 -107
  183. data/lib/polars/string_name_space.rb +781 -109
  184. data/lib/polars/struct_expr.rb +139 -18
  185. data/lib/polars/struct_name_space.rb +19 -1
  186. data/lib/polars/testing.rb +24 -273
  187. data/lib/polars/utils/constants.rb +2 -0
  188. data/lib/polars/utils/construction/data_frame.rb +410 -0
  189. data/lib/polars/utils/construction/series.rb +350 -0
  190. data/lib/polars/utils/construction/utils.rb +9 -0
  191. data/lib/polars/utils/convert.rb +18 -8
  192. data/lib/polars/utils/deprecation.rb +11 -0
  193. data/lib/polars/utils/parse.rb +62 -9
  194. data/lib/polars/utils/reduce_balanced.rb +43 -0
  195. data/lib/polars/utils/serde.rb +22 -0
  196. data/lib/polars/utils/unstable.rb +19 -0
  197. data/lib/polars/utils/various.rb +86 -1
  198. data/lib/polars/utils.rb +63 -48
  199. data/lib/polars/version.rb +1 -1
  200. data/lib/polars.rb +85 -2
  201. metadata +80 -28
  202. data/ext/polars/src/allocator.rs +0 -13
  203. data/ext/polars/src/batched_csv.rs +0 -138
  204. data/ext/polars/src/functions/string_cache.rs +0 -25
  205. data/ext/polars/src/map/dataframe.rs +0 -338
  206. data/lib/polars/plot.rb +0 -109
@@ -18,6 +18,71 @@ module Polars
18
18
  s[item]
19
19
  end
20
20
 
21
+ # Offset by `n` business days.
22
+ #
23
+ # @note
24
+ # This functionality is considered **unstable**. It may be changed
25
+ # at any point without it being considered a breaking change.
26
+ #
27
+ # @param n [Object]
28
+ # Number of business days to offset by. Can be a single number of an
29
+ # expression.
30
+ # @param week_mask [Array]
31
+ # Which days of the week to count. The default is Monday to Friday.
32
+ # If you wanted to count only Monday to Thursday, you would pass
33
+ # `[true, true, true, true, false, false, false]`.
34
+ # @param holidays [Object]
35
+ # Holidays to exclude from the count.
36
+ # roll
37
+ # What to do when the start date lands on a non-business day. Options are:
38
+ #
39
+ # - `'raise'`: raise an error
40
+ # - `'forward'`: move to the next business day
41
+ # - `'backward'`: move to the previous business day
42
+ #
43
+ # @return [Series]
44
+ #
45
+ # @example
46
+ # s = Polars::Series.new("start", [Date.new(2020, 1, 1), Date.new(2020, 1, 2)])
47
+ # s.dt.add_business_days(5)
48
+ # # =>
49
+ # # shape: (2,)
50
+ # # Series: 'start' [date]
51
+ # # [
52
+ # # 2020-01-08
53
+ # # 2020-01-09
54
+ # # ]
55
+ #
56
+ # @example You can pass a custom weekend - for example, if you only take Sunday off:
57
+ # week_mask = [true, true, true, true, true, true, false]
58
+ # s.dt.add_business_days(5, week_mask: week_mask)
59
+ # # =>
60
+ # # shape: (2,)
61
+ # # Series: 'start' [date]
62
+ # # [
63
+ # # 2020-01-07
64
+ # # 2020-01-08
65
+ # # ]
66
+ #
67
+ # @example Roll all dates forwards to the next business day:
68
+ # s = Polars::Series.new("start", [Date.new(2020, 1, 5), Date.new(2020, 1, 6)])
69
+ # s.dt.add_business_days(0, roll: "forward")
70
+ # # =>
71
+ # # shape: (2,)
72
+ # # Series: 'start' [date]
73
+ # # [
74
+ # # 2020-01-06
75
+ # # 2020-01-06
76
+ # # ]
77
+ def add_business_days(
78
+ n,
79
+ week_mask: [true, true, true, true, true, false, false],
80
+ holidays: [],
81
+ roll: "raise"
82
+ )
83
+ super
84
+ end
85
+
21
86
  # Return minimum as Ruby object.
22
87
  #
23
88
  # @return [Object]
@@ -42,49 +107,6 @@ module Polars
42
107
  Utils.wrap_s(_s).max
43
108
  end
44
109
 
45
- # Return median as Ruby object.
46
- #
47
- # @return [Object]
48
- #
49
- # @example
50
- # date = Polars.datetime_range(
51
- # DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 3), "1d", eager: true
52
- # ).alias("datetime")
53
- # # =>
54
- # # shape: (3,)
55
- # # Series: 'datetime' [datetime[ns]]
56
- # # [
57
- # # 2001-01-01 00:00:00
58
- # # 2001-01-02 00:00:00
59
- # # 2001-01-03 00:00:00
60
- # # ]
61
- #
62
- # @example
63
- # date.dt.median
64
- # # => 2001-01-02 00:00:00 UTC
65
- def median
66
- _s.median
67
- end
68
-
69
- # Return mean as Ruby object.
70
- #
71
- # @return [Object]
72
- #
73
- # @example
74
- # s = Polars::Series.new([Date.new(2001, 1, 1), Date.new(2001, 1, 2)])
75
- # s.dt.mean
76
- # # => 2001-01-01 12:00:00 UTC
77
- #
78
- # @example
79
- # s = Polars::Series.new(
80
- # [DateTime.new(2001, 1, 1), DateTime.new(2001, 1, 2), DateTime.new(2001, 1, 3)]
81
- # )
82
- # s.dt.mean
83
- # # => 2001-01-02 00:00:00 UTC
84
- def mean
85
- _s.mean
86
- end
87
-
88
110
  # Convert a Date/Time/Datetime column into a String column with the given format.
89
111
  #
90
112
  # Similar to `cast(Polars::String)`, but this method allows you to customize the
@@ -111,13 +133,18 @@ module Polars
111
133
  # # "2020/04/01"
112
134
  # # "2020/05/01"
113
135
  # # ]
114
- def to_string(format)
136
+ def to_string(format = nil)
115
137
  super
116
138
  end
117
139
 
118
- # Format Date/datetime with a formatting rule.
140
+ # Convert a Date/Time/Datetime column into a String column with the given format.
119
141
  #
120
- # See [chrono strftime/strptime](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html).
142
+ # Similar to `cast(Polars::String)`, but this method allows you to customize the
143
+ # formatting of the resulting string.
144
+ #
145
+ # @param format [String]
146
+ # Format to use, refer to the [chrono strftime documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)
147
+ # for specification. Example: `"%y-%m-%d"`.
121
148
  #
122
149
  # @return [Series]
123
150
  #
@@ -135,7 +162,75 @@ module Polars
135
162
  # # "2020/04/01"
136
163
  # # "2020/05/01"
137
164
  # # ]
138
- def strftime(fmt)
165
+ def strftime(format)
166
+ super
167
+ end
168
+
169
+ # Extract the millennium from underlying representation.
170
+ #
171
+ # Applies to Date and Datetime columns.
172
+ #
173
+ # Returns the millennium number in the calendar date.
174
+ #
175
+ # @return [Series]
176
+ #
177
+ # @example
178
+ # s = Polars::Series.new(
179
+ # "dt",
180
+ # [
181
+ # Date.new(999, 12, 31),
182
+ # Date.new(1897, 5, 7),
183
+ # Date.new(2000, 1, 1),
184
+ # Date.new(2001, 7, 5),
185
+ # Date.new(3002, 10, 20)
186
+ # ]
187
+ # )
188
+ # s.dt.millennium
189
+ # # =>
190
+ # # shape: (5,)
191
+ # # Series: 'dt' [i32]
192
+ # # [
193
+ # # 1
194
+ # # 2
195
+ # # 2
196
+ # # 3
197
+ # # 4
198
+ # # ]
199
+ def millennium
200
+ super
201
+ end
202
+
203
+ # Extract the century from underlying representation.
204
+ #
205
+ # Applies to Date and Datetime columns.
206
+ #
207
+ # Returns the century number in the calendar date.
208
+ #
209
+ # @return [Series]
210
+ #
211
+ # @example
212
+ # s = Polars::Series.new(
213
+ # "dt",
214
+ # [
215
+ # Date.new(999, 12, 31),
216
+ # Date.new(1897, 5, 7),
217
+ # Date.new(2000, 1, 1),
218
+ # Date.new(2001, 7, 5),
219
+ # Date.new(3002, 10, 20)
220
+ # ]
221
+ # )
222
+ # s.dt.century
223
+ # # =>
224
+ # # shape: (5,)
225
+ # # Series: 'dt' [i32]
226
+ # # [
227
+ # # 10
228
+ # # 19
229
+ # # 20
230
+ # # 21
231
+ # # 31
232
+ # # ]
233
+ def century
139
234
  super
140
235
  end
141
236
 
@@ -161,6 +256,72 @@ module Polars
161
256
  super
162
257
  end
163
258
 
259
+ # Determine whether each day lands on a business day.
260
+ #
261
+ # @note
262
+ # This functionality is considered **unstable**. It may be changed
263
+ # at any point without it being considered a breaking change.
264
+ #
265
+ # @param week_mask [Array]
266
+ # Which days of the week to count. The default is Monday to Friday.
267
+ # If you wanted to count only Monday to Thursday, you would pass
268
+ # `[true, true, true, true, false, false, false]`.
269
+ # @param holidays [Object]
270
+ # Holidays to exclude from the count.
271
+ #
272
+ # @return [Series]
273
+ #
274
+ # @example
275
+ # s = Polars::Series.new([Date.new(2020, 1, 3), Date.new(2020, 1, 5)])
276
+ # s.dt.is_business_day
277
+ # # =>
278
+ # # shape: (2,)
279
+ # # Series: '' [bool]
280
+ # # [
281
+ # # true
282
+ # # false
283
+ # # ]
284
+ #
285
+ # @example You can pass a custom weekend - for example, if you only take Sunday off:
286
+ # week_mask = [true, true, true, true, true, true, false]
287
+ # s.dt.is_business_day(week_mask: week_mask)
288
+ # # =>
289
+ # # shape: (2,)
290
+ # # Series: '' [bool]
291
+ # # [
292
+ # # true
293
+ # # false
294
+ # # ]
295
+ def is_business_day(
296
+ week_mask: [true, true, true, true, true, false, false],
297
+ holidays: []
298
+ )
299
+ super
300
+ end
301
+
302
+ # Determine whether the year of the underlying date representation is a leap year.
303
+ #
304
+ # Applies to Date and Datetime columns.
305
+ #
306
+ # @return [Series]
307
+ #
308
+ # @example
309
+ # s = Polars::Series.new(
310
+ # "date", [Date.new(2000, 1, 1), Date.new(2001, 1, 1), Date.new(2002, 1, 1)]
311
+ # )
312
+ # s.dt.is_leap_year
313
+ # # =>
314
+ # # shape: (3,)
315
+ # # Series: 'date' [bool]
316
+ # # [
317
+ # # true
318
+ # # false
319
+ # # false
320
+ # # ]
321
+ def is_leap_year
322
+ super
323
+ end
324
+
164
325
  # Extract ISO year from underlying Date representation.
165
326
  #
166
327
  # Applies to Date and Datetime columns.
@@ -236,6 +397,32 @@ module Polars
236
397
  super
237
398
  end
238
399
 
400
+ # Extract the number of days in the month from the underlying date representation.
401
+ #
402
+ # Applies to Date and Datetime columns.
403
+ #
404
+ # Returns the number of days in the month.
405
+ # The return value ranges from 28 to 31.
406
+ #
407
+ # @return [Series]
408
+ #
409
+ # @example
410
+ # s = Polars::Series.new(
411
+ # "date", [Date.new(2001, 1, 1), Date.new(2001, 2, 1), Date.new(2000, 2, 1)]
412
+ # )
413
+ # s.dt.days_in_month
414
+ # # =>
415
+ # # shape: (3,)
416
+ # # Series: 'date' [i8]
417
+ # # [
418
+ # # 31
419
+ # # 28
420
+ # # 29
421
+ # # ]
422
+ def days_in_month
423
+ super
424
+ end
425
+
239
426
  # Extract the week from the underlying date representation.
240
427
  #
241
428
  # Applies to Date and Datetime columns.
@@ -346,6 +533,48 @@ module Polars
346
533
  super
347
534
  end
348
535
 
536
+ # Extract (local) time.
537
+ #
538
+ # Applies to Date/Datetime/Time columns.
539
+ #
540
+ # @return
541
+ #
542
+ # @example
543
+ # ser = Polars::Series.new([DateTime.new(2021, 1, 2, 5)]).dt.replace_time_zone(
544
+ # "Asia/Kathmandu"
545
+ # )
546
+ # ser.dt.time
547
+ # # =>
548
+ # # shape: (1,)
549
+ # # Series: '' [time]
550
+ # # [
551
+ # # 05:00:00
552
+ # # ]
553
+ def time
554
+ super
555
+ end
556
+
557
+ # Extract (local) date.
558
+ #
559
+ # Applies to Date/Datetime columns.
560
+ #
561
+ # @return [Series]
562
+ #
563
+ # @example
564
+ # ser = Polars::Series.new([DateTime.new(2021, 1, 2, 5)]).dt.replace_time_zone(
565
+ # "Asia/Kathmandu"
566
+ # )
567
+ # ser.dt.date
568
+ # # =>
569
+ # # shape: (1,)
570
+ # # Series: '' [date]
571
+ # # [
572
+ # # 2021-01-02
573
+ # # ]
574
+ def date
575
+ super
576
+ end
577
+
349
578
  # Extract the hour from the underlying DateTime representation.
350
579
  #
351
580
  # Applies to Datetime columns.
@@ -624,43 +853,6 @@ module Polars
624
853
  super
625
854
  end
626
855
 
627
- # Set time unit a Series of dtype Datetime or Duration.
628
- #
629
- # This does not modify underlying data, and should be used to fix an incorrect
630
- # time unit.
631
- #
632
- # @param time_unit ["ns", "us", "ms"]
633
- # Time unit for the `Datetime` Series.
634
- #
635
- # @return [Series]
636
- #
637
- # @example
638
- # start = DateTime.new(2001, 1, 1)
639
- # stop = DateTime.new(2001, 1, 3)
640
- # date = Polars.datetime_range(start, stop, "1d", time_unit: "ns", eager: true).alias("datetime")
641
- # # =>
642
- # # shape: (3,)
643
- # # Series: 'datetime' [datetime[ns]]
644
- # # [
645
- # # 2001-01-01 00:00:00
646
- # # 2001-01-02 00:00:00
647
- # # 2001-01-03 00:00:00
648
- # # ]
649
- #
650
- # @example
651
- # date.dt.with_time_unit("us").alias("tu_us")
652
- # # =>
653
- # # shape: (3,)
654
- # # Series: 'tu_us' [datetime[μs]]
655
- # # [
656
- # # +32971-04-28 00:00:00
657
- # # +32974-01-22 00:00:00
658
- # # +32976-10-18 00:00:00
659
- # # ]
660
- def with_time_unit(time_unit)
661
- super
662
- end
663
-
664
856
  # Cast the underlying data to another time unit. This may lose precision.
665
857
  #
666
858
  # @param time_unit ["ns", "us", "ms"]
@@ -825,21 +1017,11 @@ module Polars
825
1017
  super
826
1018
  end
827
1019
 
828
- # Localize tz-naive Datetime Series to tz-aware Datetime Series.
829
- #
830
- # This method takes a naive Datetime Series and makes this time zone aware.
831
- # It does not move the time to another time zone.
832
- #
833
- # @param tz [String]
834
- # Time zone for the `Datetime` Series.
835
- #
836
- # @return [Series]
837
- def tz_localize(tz)
838
- super
839
- end
840
-
841
1020
  # Extract the days from a Duration type.
842
1021
  #
1022
+ # @param fractional [Boolean]
1023
+ # Whether to include the fractional component of the day.
1024
+ #
843
1025
  # @return [Series]
844
1026
  #
845
1027
  # @example
@@ -855,13 +1037,15 @@ module Polars
855
1037
  # # 31
856
1038
  # # 30
857
1039
  # # ]
858
- def total_days
1040
+ def total_days(fractional: false)
859
1041
  super
860
1042
  end
861
- alias_method :days, :total_days
862
1043
 
863
1044
  # Extract the hours from a Duration type.
864
1045
  #
1046
+ # @param fractional [Boolean]
1047
+ # Whether to include the fractional component of the hour.
1048
+ #
865
1049
  # @return [Series]
866
1050
  #
867
1051
  # @example
@@ -887,13 +1071,15 @@ module Polars
887
1071
  # # 24
888
1072
  # # 24
889
1073
  # # ]
890
- def total_hours
1074
+ def total_hours(fractional: false)
891
1075
  super
892
1076
  end
893
- alias_method :hours, :total_hours
894
1077
 
895
1078
  # Extract the minutes from a Duration type.
896
1079
  #
1080
+ # @param fractional [Boolean]
1081
+ # Whether to include the fractional component of the minute.
1082
+ #
897
1083
  # @return [Series]
898
1084
  #
899
1085
  # @example
@@ -919,13 +1105,15 @@ module Polars
919
1105
  # # 1440
920
1106
  # # 1440
921
1107
  # # ]
922
- def total_minutes
1108
+ def total_minutes(fractional: false)
923
1109
  super
924
1110
  end
925
- alias_method :minutes, :total_minutes
926
1111
 
927
1112
  # Extract the seconds from a Duration type.
928
1113
  #
1114
+ # @param fractional [Boolean]
1115
+ # Whether to include the fractional component of the second.
1116
+ #
929
1117
  # @return [Series]
930
1118
  #
931
1119
  # @example
@@ -955,13 +1143,15 @@ module Polars
955
1143
  # # 60
956
1144
  # # 60
957
1145
  # # ]
958
- def total_seconds
1146
+ def total_seconds(fractional: false)
959
1147
  super
960
1148
  end
961
- alias_method :seconds, :total_seconds
962
1149
 
963
1150
  # Extract the milliseconds from a Duration type.
964
1151
  #
1152
+ # @param fractional [Boolean]
1153
+ # Whether to include the fractional component of the millisecond.
1154
+ #
965
1155
  # @return [Series]
966
1156
  #
967
1157
  # @example
@@ -987,13 +1177,15 @@ module Polars
987
1177
  # # 1
988
1178
  # # 1
989
1179
  # # ]
990
- def total_milliseconds
1180
+ def total_milliseconds(fractional: false)
991
1181
  super
992
1182
  end
993
- alias_method :milliseconds, :total_milliseconds
994
1183
 
995
1184
  # Extract the microseconds from a Duration type.
996
1185
  #
1186
+ # @param fractional [Boolean]
1187
+ # Whether to include the fractional component of the microsecond.
1188
+ #
997
1189
  # @return [Series]
998
1190
  #
999
1191
  # @example
@@ -1019,13 +1211,17 @@ module Polars
1019
1211
  # # 1000
1020
1212
  # # 1000
1021
1213
  # # ]
1022
- def total_microseconds
1214
+ def total_microseconds(fractional: false)
1023
1215
  super
1024
1216
  end
1025
- alias_method :microseconds, :total_microseconds
1026
1217
 
1027
1218
  # Extract the nanoseconds from a Duration type.
1028
1219
  #
1220
+ # @param fractional [Boolean]
1221
+ # Whether to include return the result as a `Float64`.
1222
+ # Because the smallest `TimeUnit` is `'ns'`, the
1223
+ # fractional component will always be zero.
1224
+ #
1029
1225
  # @return [Series]
1030
1226
  #
1031
1227
  # @example
@@ -1051,10 +1247,9 @@ module Polars
1051
1247
  # # 1000000
1052
1248
  # # 1000000
1053
1249
  # # ]
1054
- def total_nanoseconds
1250
+ def total_nanoseconds(fractional: false)
1055
1251
  super
1056
1252
  end
1057
- alias_method :nanoseconds, :total_nanoseconds
1058
1253
 
1059
1254
  # Offset this date by a relative time offset.
1060
1255
  #
@@ -1276,6 +1471,35 @@ module Polars
1276
1471
  super
1277
1472
  end
1278
1473
 
1474
+ # Create a naive Datetime from an existing Date/Datetime expression and a Time.
1475
+ #
1476
+ # If the underlying expression is a Datetime then its time component is replaced,
1477
+ # and if it is a Date then a new Datetime is created by combining the two values.
1478
+ #
1479
+ # @param time [Object]
1480
+ # A Ruby time literal or Series of the same length as this Series.
1481
+ # @param time_unit ['ns', 'us', 'ms']
1482
+ # Unit of time.
1483
+ #
1484
+ # @return [Series]
1485
+ #
1486
+ # @example
1487
+ # s = Polars::Series.new(
1488
+ # "dtm",
1489
+ # [DateTime.new(2022, 12, 31, 10, 30, 45), DateTime.new(2023, 7, 5, 23, 59, 59)]
1490
+ # )
1491
+ # s.dt.combine(Time.new(2000, 1, 1, 1, 2, 3.456))
1492
+ # # =>
1493
+ # # shape: (2,)
1494
+ # # Series: 'dtm' [datetime[μs]]
1495
+ # # [
1496
+ # # 2022-12-31 01:02:03.456
1497
+ # # 2023-07-05 01:02:03.456
1498
+ # # ]
1499
+ def combine(time, time_unit: "us")
1500
+ super
1501
+ end
1502
+
1279
1503
  # Roll backward to the first day of the month.
1280
1504
  #
1281
1505
  # @return [Series]
@@ -1370,5 +1594,54 @@ module Polars
1370
1594
  def dst_offset
1371
1595
  super
1372
1596
  end
1597
+
1598
+ # Replace time unit.
1599
+ #
1600
+ # @param year [Object]
1601
+ # Literal or Series.
1602
+ # @param month [Object]
1603
+ # Literal or Series, ranging from 1-12.
1604
+ # @param day [Object]
1605
+ # Literal or Series, ranging from 1-31.
1606
+ # @param hour [Object]
1607
+ # Literal or Series, ranging from 0-23.
1608
+ # @param minute [Object]
1609
+ # Literal or Series, ranging from 0-59.
1610
+ # @param second [Object]
1611
+ # Literal or Series, ranging from 0-59.
1612
+ # @param microsecond [Object]
1613
+ # Literal or Series, ranging from 0-999999.
1614
+ # @param ambiguous [String]
1615
+ # Determine how to deal with ambiguous datetimes:
1616
+ #
1617
+ # - `'raise'` (default): raise
1618
+ # - `'earliest'`: use the earliest datetime
1619
+ # - `'latest'`: use the latest datetime
1620
+ # - `'null'`: set to null
1621
+ #
1622
+ # @return [Series]
1623
+ #
1624
+ # @example
1625
+ # s = Polars::Series.new("date", [Date.new(2013, 1, 1), Date.new(2024, 1, 2)])
1626
+ # s.dt.replace(year: 1800)
1627
+ # # =>
1628
+ # # shape: (2,)
1629
+ # # Series: 'date' [date]
1630
+ # # [
1631
+ # # 1800-01-01
1632
+ # # 1800-01-02
1633
+ # # ]
1634
+ def replace(
1635
+ year: nil,
1636
+ month: nil,
1637
+ day: nil,
1638
+ hour: nil,
1639
+ minute: nil,
1640
+ second: nil,
1641
+ microsecond: nil,
1642
+ ambiguous: "raise"
1643
+ )
1644
+ super
1645
+ end
1373
1646
  end
1374
1647
  end