polars-runtime-compat 1.34.0b3__cp39-abi3-win_amd64.whl → 1.34.0b4__cp39-abi3-win_amd64.whl

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.

Potentially problematic release.


This version of polars-runtime-compat might be problematic. Click here for more details.

Files changed (203) hide show
  1. _polars_runtime_compat/_polars_runtime_compat.pyd +0 -0
  2. {polars_runtime_compat-1.34.0b3.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/METADATA +1 -1
  3. polars_runtime_compat-1.34.0b4.dist-info/RECORD +6 -0
  4. polars/__init__.py +0 -528
  5. polars/_cpu_check.py +0 -265
  6. polars/_dependencies.py +0 -355
  7. polars/_plr.py +0 -99
  8. polars/_plr.pyi +0 -2496
  9. polars/_reexport.py +0 -23
  10. polars/_typing.py +0 -478
  11. polars/_utils/__init__.py +0 -37
  12. polars/_utils/async_.py +0 -102
  13. polars/_utils/cache.py +0 -176
  14. polars/_utils/cloud.py +0 -40
  15. polars/_utils/constants.py +0 -29
  16. polars/_utils/construction/__init__.py +0 -46
  17. polars/_utils/construction/dataframe.py +0 -1397
  18. polars/_utils/construction/other.py +0 -72
  19. polars/_utils/construction/series.py +0 -560
  20. polars/_utils/construction/utils.py +0 -118
  21. polars/_utils/convert.py +0 -224
  22. polars/_utils/deprecation.py +0 -406
  23. polars/_utils/getitem.py +0 -457
  24. polars/_utils/logging.py +0 -11
  25. polars/_utils/nest_asyncio.py +0 -264
  26. polars/_utils/parquet.py +0 -15
  27. polars/_utils/parse/__init__.py +0 -12
  28. polars/_utils/parse/expr.py +0 -242
  29. polars/_utils/polars_version.py +0 -19
  30. polars/_utils/pycapsule.py +0 -53
  31. polars/_utils/scan.py +0 -27
  32. polars/_utils/serde.py +0 -63
  33. polars/_utils/slice.py +0 -215
  34. polars/_utils/udfs.py +0 -1251
  35. polars/_utils/unstable.py +0 -63
  36. polars/_utils/various.py +0 -782
  37. polars/_utils/wrap.py +0 -25
  38. polars/api.py +0 -370
  39. polars/catalog/__init__.py +0 -0
  40. polars/catalog/unity/__init__.py +0 -19
  41. polars/catalog/unity/client.py +0 -733
  42. polars/catalog/unity/models.py +0 -152
  43. polars/config.py +0 -1571
  44. polars/convert/__init__.py +0 -25
  45. polars/convert/general.py +0 -1046
  46. polars/convert/normalize.py +0 -261
  47. polars/dataframe/__init__.py +0 -5
  48. polars/dataframe/_html.py +0 -186
  49. polars/dataframe/frame.py +0 -12582
  50. polars/dataframe/group_by.py +0 -1067
  51. polars/dataframe/plotting.py +0 -257
  52. polars/datatype_expr/__init__.py +0 -5
  53. polars/datatype_expr/array.py +0 -56
  54. polars/datatype_expr/datatype_expr.py +0 -304
  55. polars/datatype_expr/list.py +0 -18
  56. polars/datatype_expr/struct.py +0 -69
  57. polars/datatypes/__init__.py +0 -122
  58. polars/datatypes/_parse.py +0 -195
  59. polars/datatypes/_utils.py +0 -48
  60. polars/datatypes/classes.py +0 -1213
  61. polars/datatypes/constants.py +0 -11
  62. polars/datatypes/constructor.py +0 -172
  63. polars/datatypes/convert.py +0 -366
  64. polars/datatypes/group.py +0 -130
  65. polars/exceptions.py +0 -230
  66. polars/expr/__init__.py +0 -7
  67. polars/expr/array.py +0 -964
  68. polars/expr/binary.py +0 -346
  69. polars/expr/categorical.py +0 -306
  70. polars/expr/datetime.py +0 -2620
  71. polars/expr/expr.py +0 -11272
  72. polars/expr/list.py +0 -1408
  73. polars/expr/meta.py +0 -444
  74. polars/expr/name.py +0 -321
  75. polars/expr/string.py +0 -3045
  76. polars/expr/struct.py +0 -357
  77. polars/expr/whenthen.py +0 -185
  78. polars/functions/__init__.py +0 -193
  79. polars/functions/aggregation/__init__.py +0 -33
  80. polars/functions/aggregation/horizontal.py +0 -298
  81. polars/functions/aggregation/vertical.py +0 -341
  82. polars/functions/as_datatype.py +0 -848
  83. polars/functions/business.py +0 -138
  84. polars/functions/col.py +0 -384
  85. polars/functions/datatype.py +0 -121
  86. polars/functions/eager.py +0 -524
  87. polars/functions/escape_regex.py +0 -29
  88. polars/functions/lazy.py +0 -2751
  89. polars/functions/len.py +0 -68
  90. polars/functions/lit.py +0 -210
  91. polars/functions/random.py +0 -22
  92. polars/functions/range/__init__.py +0 -19
  93. polars/functions/range/_utils.py +0 -15
  94. polars/functions/range/date_range.py +0 -303
  95. polars/functions/range/datetime_range.py +0 -370
  96. polars/functions/range/int_range.py +0 -348
  97. polars/functions/range/linear_space.py +0 -311
  98. polars/functions/range/time_range.py +0 -287
  99. polars/functions/repeat.py +0 -301
  100. polars/functions/whenthen.py +0 -353
  101. polars/interchange/__init__.py +0 -10
  102. polars/interchange/buffer.py +0 -77
  103. polars/interchange/column.py +0 -190
  104. polars/interchange/dataframe.py +0 -230
  105. polars/interchange/from_dataframe.py +0 -328
  106. polars/interchange/protocol.py +0 -303
  107. polars/interchange/utils.py +0 -170
  108. polars/io/__init__.py +0 -64
  109. polars/io/_utils.py +0 -317
  110. polars/io/avro.py +0 -49
  111. polars/io/clipboard.py +0 -36
  112. polars/io/cloud/__init__.py +0 -17
  113. polars/io/cloud/_utils.py +0 -80
  114. polars/io/cloud/credential_provider/__init__.py +0 -17
  115. polars/io/cloud/credential_provider/_builder.py +0 -520
  116. polars/io/cloud/credential_provider/_providers.py +0 -618
  117. polars/io/csv/__init__.py +0 -9
  118. polars/io/csv/_utils.py +0 -38
  119. polars/io/csv/batched_reader.py +0 -142
  120. polars/io/csv/functions.py +0 -1495
  121. polars/io/database/__init__.py +0 -6
  122. polars/io/database/_arrow_registry.py +0 -70
  123. polars/io/database/_cursor_proxies.py +0 -147
  124. polars/io/database/_executor.py +0 -578
  125. polars/io/database/_inference.py +0 -314
  126. polars/io/database/_utils.py +0 -144
  127. polars/io/database/functions.py +0 -516
  128. polars/io/delta.py +0 -499
  129. polars/io/iceberg/__init__.py +0 -3
  130. polars/io/iceberg/_utils.py +0 -697
  131. polars/io/iceberg/dataset.py +0 -556
  132. polars/io/iceberg/functions.py +0 -151
  133. polars/io/ipc/__init__.py +0 -8
  134. polars/io/ipc/functions.py +0 -514
  135. polars/io/json/__init__.py +0 -3
  136. polars/io/json/read.py +0 -101
  137. polars/io/ndjson.py +0 -332
  138. polars/io/parquet/__init__.py +0 -17
  139. polars/io/parquet/field_overwrites.py +0 -140
  140. polars/io/parquet/functions.py +0 -722
  141. polars/io/partition.py +0 -491
  142. polars/io/plugins.py +0 -187
  143. polars/io/pyarrow_dataset/__init__.py +0 -5
  144. polars/io/pyarrow_dataset/anonymous_scan.py +0 -109
  145. polars/io/pyarrow_dataset/functions.py +0 -79
  146. polars/io/scan_options/__init__.py +0 -5
  147. polars/io/scan_options/_options.py +0 -59
  148. polars/io/scan_options/cast_options.py +0 -126
  149. polars/io/spreadsheet/__init__.py +0 -6
  150. polars/io/spreadsheet/_utils.py +0 -52
  151. polars/io/spreadsheet/_write_utils.py +0 -647
  152. polars/io/spreadsheet/functions.py +0 -1323
  153. polars/lazyframe/__init__.py +0 -9
  154. polars/lazyframe/engine_config.py +0 -61
  155. polars/lazyframe/frame.py +0 -8564
  156. polars/lazyframe/group_by.py +0 -669
  157. polars/lazyframe/in_process.py +0 -42
  158. polars/lazyframe/opt_flags.py +0 -333
  159. polars/meta/__init__.py +0 -14
  160. polars/meta/build.py +0 -33
  161. polars/meta/index_type.py +0 -27
  162. polars/meta/thread_pool.py +0 -50
  163. polars/meta/versions.py +0 -120
  164. polars/ml/__init__.py +0 -0
  165. polars/ml/torch.py +0 -213
  166. polars/ml/utilities.py +0 -30
  167. polars/plugins.py +0 -155
  168. polars/py.typed +0 -0
  169. polars/pyproject.toml +0 -103
  170. polars/schema.py +0 -265
  171. polars/selectors.py +0 -3117
  172. polars/series/__init__.py +0 -5
  173. polars/series/array.py +0 -776
  174. polars/series/binary.py +0 -254
  175. polars/series/categorical.py +0 -246
  176. polars/series/datetime.py +0 -2275
  177. polars/series/list.py +0 -1087
  178. polars/series/plotting.py +0 -191
  179. polars/series/series.py +0 -9197
  180. polars/series/string.py +0 -2367
  181. polars/series/struct.py +0 -154
  182. polars/series/utils.py +0 -191
  183. polars/sql/__init__.py +0 -7
  184. polars/sql/context.py +0 -677
  185. polars/sql/functions.py +0 -139
  186. polars/string_cache.py +0 -185
  187. polars/testing/__init__.py +0 -13
  188. polars/testing/asserts/__init__.py +0 -9
  189. polars/testing/asserts/frame.py +0 -231
  190. polars/testing/asserts/series.py +0 -219
  191. polars/testing/asserts/utils.py +0 -12
  192. polars/testing/parametric/__init__.py +0 -33
  193. polars/testing/parametric/profiles.py +0 -107
  194. polars/testing/parametric/strategies/__init__.py +0 -22
  195. polars/testing/parametric/strategies/_utils.py +0 -14
  196. polars/testing/parametric/strategies/core.py +0 -615
  197. polars/testing/parametric/strategies/data.py +0 -452
  198. polars/testing/parametric/strategies/dtype.py +0 -436
  199. polars/testing/parametric/strategies/legacy.py +0 -169
  200. polars/type_aliases.py +0 -24
  201. polars_runtime_compat-1.34.0b3.dist-info/RECORD +0 -203
  202. {polars_runtime_compat-1.34.0b3.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/WHEEL +0 -0
  203. {polars_runtime_compat-1.34.0b3.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/licenses/LICENSE +0 -0
@@ -1,848 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import contextlib
4
- from typing import TYPE_CHECKING, overload
5
-
6
- from polars import functions as F
7
- from polars._utils.parse import (
8
- parse_into_expression,
9
- parse_into_list_of_expressions,
10
- )
11
- from polars._utils.unstable import issue_unstable_warning
12
- from polars._utils.wrap import wrap_expr
13
- from polars.datatypes import Date, Struct, Time
14
-
15
- with contextlib.suppress(ImportError): # Module not available when building docs
16
- import polars._plr as plr
17
-
18
-
19
- if TYPE_CHECKING:
20
- from collections.abc import Iterable
21
- from typing import Literal
22
-
23
- from polars import Expr, Series
24
- from polars._typing import Ambiguous, IntoExpr, SchemaDict, TimeUnit
25
-
26
-
27
- def datetime_(
28
- year: int | IntoExpr,
29
- month: int | IntoExpr,
30
- day: int | IntoExpr,
31
- hour: int | IntoExpr | None = None,
32
- minute: int | IntoExpr | None = None,
33
- second: int | IntoExpr | None = None,
34
- microsecond: int | IntoExpr | None = None,
35
- *,
36
- time_unit: TimeUnit = "us",
37
- time_zone: str | None = None,
38
- ambiguous: Ambiguous | Expr = "raise",
39
- ) -> Expr:
40
- """
41
- Create a Polars literal expression of type Datetime.
42
-
43
- Parameters
44
- ----------
45
- year
46
- Column or literal.
47
- month
48
- Column or literal, ranging from 1-12.
49
- day
50
- Column or literal, ranging from 1-31.
51
- hour
52
- Column or literal, ranging from 0-23.
53
- minute
54
- Column or literal, ranging from 0-59.
55
- second
56
- Column or literal, ranging from 0-59.
57
- microsecond
58
- Column or literal, ranging from 0-999999.
59
- time_unit : {'us', 'ms', 'ns'}
60
- Time unit of the resulting expression.
61
- time_zone
62
- Time zone of the resulting expression.
63
- ambiguous
64
- Determine how to deal with ambiguous datetimes:
65
-
66
- - `'raise'` (default): raise
67
- - `'earliest'`: use the earliest datetime
68
- - `'latest'`: use the latest datetime
69
- - `'null'`: set to null
70
-
71
- Returns
72
- -------
73
- Expr
74
- Expression of data type :class:`Datetime`.
75
-
76
- Examples
77
- --------
78
- >>> df = pl.DataFrame(
79
- ... {
80
- ... "month": [1, 2, 3],
81
- ... "day": [4, 5, 6],
82
- ... "hour": [12, 13, 14],
83
- ... "minute": [15, 30, 45],
84
- ... }
85
- ... )
86
- >>> df.with_columns(
87
- ... pl.datetime(
88
- ... 2024,
89
- ... pl.col("month"),
90
- ... pl.col("day"),
91
- ... pl.col("hour"),
92
- ... pl.col("minute"),
93
- ... time_zone="Australia/Sydney",
94
- ... )
95
- ... )
96
- shape: (3, 5)
97
- ┌───────┬─────┬──────┬────────┬────────────────────────────────┐
98
- │ month ┆ day ┆ hour ┆ minute ┆ datetime │
99
- │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
100
- │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ datetime[μs, Australia/Sydney] │
101
- ╞═══════╪═════╪══════╪════════╪════════════════════════════════╡
102
- │ 1 ┆ 4 ┆ 12 ┆ 15 ┆ 2024-01-04 12:15:00 AEDT │
103
- │ 2 ┆ 5 ┆ 13 ┆ 30 ┆ 2024-02-05 13:30:00 AEDT │
104
- │ 3 ┆ 6 ┆ 14 ┆ 45 ┆ 2024-03-06 14:45:00 AEDT │
105
- └───────┴─────┴──────┴────────┴────────────────────────────────┘
106
-
107
- We can also use `pl.datetime` for filtering:
108
-
109
- >>> from datetime import datetime
110
- >>> df = pl.DataFrame(
111
- ... {
112
- ... "start": [
113
- ... datetime(2024, 1, 1, 0, 0, 0),
114
- ... datetime(2024, 1, 1, 0, 0, 0),
115
- ... datetime(2024, 1, 1, 0, 0, 0),
116
- ... ],
117
- ... "end": [
118
- ... datetime(2024, 5, 1, 20, 15, 10),
119
- ... datetime(2024, 7, 1, 21, 25, 20),
120
- ... datetime(2024, 9, 1, 22, 35, 30),
121
- ... ],
122
- ... }
123
- ... )
124
- >>> df.filter(pl.col("end") > pl.datetime(2024, 6, 1))
125
- shape: (2, 2)
126
- ┌─────────────────────┬─────────────────────┐
127
- │ start ┆ end │
128
- │ --- ┆ --- │
129
- │ datetime[μs] ┆ datetime[μs] │
130
- ╞═════════════════════╪═════════════════════╡
131
- │ 2024-01-01 00:00:00 ┆ 2024-07-01 21:25:20 │
132
- │ 2024-01-01 00:00:00 ┆ 2024-09-01 22:35:30 │
133
- └─────────────────────┴─────────────────────┘
134
- """
135
- ambiguous_expr = parse_into_expression(ambiguous, str_as_lit=True)
136
- year_expr = parse_into_expression(year)
137
- month_expr = parse_into_expression(month)
138
- day_expr = parse_into_expression(day)
139
-
140
- hour_expr = parse_into_expression(hour) if hour is not None else None
141
- minute_expr = parse_into_expression(minute) if minute is not None else None
142
- second_expr = parse_into_expression(second) if second is not None else None
143
- microsecond_expr = (
144
- parse_into_expression(microsecond) if microsecond is not None else None
145
- )
146
-
147
- return wrap_expr(
148
- plr.datetime(
149
- year_expr,
150
- month_expr,
151
- day_expr,
152
- hour_expr,
153
- minute_expr,
154
- second_expr,
155
- microsecond_expr,
156
- time_unit,
157
- time_zone,
158
- ambiguous_expr,
159
- )
160
- )
161
-
162
-
163
- def date_(
164
- year: Expr | str | int,
165
- month: Expr | str | int,
166
- day: Expr | str | int,
167
- ) -> Expr:
168
- """
169
- Create a Polars literal expression of type Date.
170
-
171
- Parameters
172
- ----------
173
- year
174
- column or literal.
175
- month
176
- column or literal, ranging from 1-12.
177
- day
178
- column or literal, ranging from 1-31.
179
-
180
- Returns
181
- -------
182
- Expr
183
- Expression of data type :class:`Date`.
184
-
185
- Examples
186
- --------
187
- >>> df = pl.DataFrame(
188
- ... {
189
- ... "month": [1, 2, 3],
190
- ... "day": [4, 5, 6],
191
- ... }
192
- ... )
193
- >>> df.with_columns(pl.date(2024, pl.col("month"), pl.col("day")))
194
- shape: (3, 3)
195
- ┌───────┬─────┬────────────┐
196
- │ month ┆ day ┆ date │
197
- │ --- ┆ --- ┆ --- │
198
- │ i64 ┆ i64 ┆ date │
199
- ╞═══════╪═════╪════════════╡
200
- │ 1 ┆ 4 ┆ 2024-01-04 │
201
- │ 2 ┆ 5 ┆ 2024-02-05 │
202
- │ 3 ┆ 6 ┆ 2024-03-06 │
203
- └───────┴─────┴────────────┘
204
-
205
- We can also use `pl.date` for filtering:
206
-
207
- >>> from datetime import date
208
- >>> df = pl.DataFrame(
209
- ... {
210
- ... "start": [date(2024, 1, 1), date(2024, 1, 1), date(2024, 1, 1)],
211
- ... "end": [date(2024, 5, 1), date(2024, 7, 1), date(2024, 9, 1)],
212
- ... }
213
- ... )
214
- >>> df.filter(pl.col("end") > pl.date(2024, 6, 1))
215
- shape: (2, 2)
216
- ┌────────────┬────────────┐
217
- │ start ┆ end │
218
- │ --- ┆ --- │
219
- │ date ┆ date │
220
- ╞════════════╪════════════╡
221
- │ 2024-01-01 ┆ 2024-07-01 │
222
- │ 2024-01-01 ┆ 2024-09-01 │
223
- └────────────┴────────────┘
224
- """
225
- return datetime_(year, month, day).cast(Date).alias("date")
226
-
227
-
228
- def time_(
229
- hour: Expr | str | int | None = None,
230
- minute: Expr | str | int | None = None,
231
- second: Expr | str | int | None = None,
232
- microsecond: Expr | str | int | None = None,
233
- ) -> Expr:
234
- """
235
- Create a Polars literal expression of type Time.
236
-
237
- Parameters
238
- ----------
239
- hour
240
- column or literal, ranging from 0-23.
241
- minute
242
- column or literal, ranging from 0-59.
243
- second
244
- column or literal, ranging from 0-59.
245
- microsecond
246
- column or literal, ranging from 0-999999.
247
-
248
- Returns
249
- -------
250
- Expr
251
- Expression of data type :class:`Date`.
252
-
253
- Examples
254
- --------
255
- >>> df = pl.DataFrame(
256
- ... {
257
- ... "hour": [12, 13, 14],
258
- ... "minute": [15, 30, 45],
259
- ... }
260
- ... )
261
-
262
- >>> df.with_columns(pl.time(pl.col("hour"), pl.col("minute")))
263
- shape: (3, 3)
264
- ┌──────┬────────┬──────────┐
265
- │ hour ┆ minute ┆ time │
266
- │ --- ┆ --- ┆ --- │
267
- │ i64 ┆ i64 ┆ time │
268
- ╞══════╪════════╪══════════╡
269
- │ 12 ┆ 15 ┆ 12:15:00 │
270
- │ 13 ┆ 30 ┆ 13:30:00 │
271
- │ 14 ┆ 45 ┆ 14:45:00 │
272
- └──────┴────────┴──────────┘
273
- """
274
- epoch_start = (1970, 1, 1)
275
- return (
276
- datetime_(*epoch_start, hour, minute, second, microsecond)
277
- .cast(Time)
278
- .alias("time")
279
- )
280
-
281
-
282
- def duration(
283
- *,
284
- weeks: Expr | str | int | float | None = None,
285
- days: Expr | str | int | float | None = None,
286
- hours: Expr | str | int | float | None = None,
287
- minutes: Expr | str | int | float | None = None,
288
- seconds: Expr | str | int | float | None = None,
289
- milliseconds: Expr | str | int | float | None = None,
290
- microseconds: Expr | str | int | float | None = None,
291
- nanoseconds: Expr | str | int | float | None = None,
292
- time_unit: TimeUnit | None = None,
293
- ) -> Expr:
294
- """
295
- Create polars `Duration` from distinct time components.
296
-
297
- Parameters
298
- ----------
299
- weeks
300
- Number of weeks.
301
- days
302
- Number of days.
303
- hours
304
- Number of hours.
305
- minutes
306
- Number of minutes.
307
- seconds
308
- Number of seconds.
309
- milliseconds
310
- Number of milliseconds.
311
- microseconds
312
- Number of microseconds.
313
- nanoseconds
314
- Number of nanoseconds.
315
- time_unit : {None, 'us', 'ms', 'ns'}
316
- Time unit of the resulting expression. If set to `None` (default), the time
317
- unit will be inferred from the other inputs: `'ns'` if `nanoseconds` was
318
- specified, `'us'` otherwise.
319
-
320
- Returns
321
- -------
322
- Expr
323
- Expression of data type :class:`Duration`.
324
-
325
- Notes
326
- -----
327
- A `duration` represents a fixed amount of time. For example,
328
- `pl.duration(days=1)` means "exactly 24 hours". By contrast,
329
- `Expr.dt.offset_by('1d')` means "1 calendar day", which could sometimes be
330
- 23 hours or 25 hours depending on Daylight Savings Time.
331
- For non-fixed durations such as "calendar month" or "calendar day",
332
- please use :meth:`polars.Expr.dt.offset_by` instead.
333
-
334
- Examples
335
- --------
336
- >>> from datetime import datetime
337
- >>> df = pl.DataFrame(
338
- ... {
339
- ... "dt": [datetime(2022, 1, 1), datetime(2022, 1, 2)],
340
- ... "add": [1, 2],
341
- ... }
342
- ... )
343
- >>> df
344
- shape: (2, 2)
345
- ┌─────────────────────┬─────┐
346
- │ dt ┆ add │
347
- │ --- ┆ --- │
348
- │ datetime[μs] ┆ i64 │
349
- ╞═════════════════════╪═════╡
350
- │ 2022-01-01 00:00:00 ┆ 1 │
351
- │ 2022-01-02 00:00:00 ┆ 2 │
352
- └─────────────────────┴─────┘
353
- >>> with pl.Config(tbl_width_chars=120):
354
- ... df.select(
355
- ... (pl.col("dt") + pl.duration(weeks="add")).alias("add_weeks"),
356
- ... (pl.col("dt") + pl.duration(days="add")).alias("add_days"),
357
- ... (pl.col("dt") + pl.duration(seconds="add")).alias("add_seconds"),
358
- ... (pl.col("dt") + pl.duration(milliseconds="add")).alias("add_millis"),
359
- ... (pl.col("dt") + pl.duration(hours="add")).alias("add_hours"),
360
- ... )
361
- shape: (2, 5)
362
- ┌─────────────────────┬─────────────────────┬─────────────────────┬─────────────────────────┬─────────────────────┐
363
- │ add_weeks ┆ add_days ┆ add_seconds ┆ add_millis ┆ add_hours │
364
- │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
365
- │ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] │
366
- ╞═════════════════════╪═════════════════════╪═════════════════════╪═════════════════════════╪═════════════════════╡
367
- │ 2022-01-08 00:00:00 ┆ 2022-01-02 00:00:00 ┆ 2022-01-01 00:00:01 ┆ 2022-01-01 00:00:00.001 ┆ 2022-01-01 01:00:00 │
368
- │ 2022-01-16 00:00:00 ┆ 2022-01-04 00:00:00 ┆ 2022-01-02 00:00:02 ┆ 2022-01-02 00:00:00.002 ┆ 2022-01-02 02:00:00 │
369
- └─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────────┴─────────────────────┘
370
-
371
- If you need to add non-fixed durations, you should use :meth:`polars.Expr.dt.offset_by` instead:
372
-
373
- >>> with pl.Config(tbl_width_chars=120):
374
- ... df.select(
375
- ... add_calendar_days=pl.col("dt").dt.offset_by(
376
- ... pl.format("{}d", pl.col("add"))
377
- ... ),
378
- ... add_calendar_months=pl.col("dt").dt.offset_by(
379
- ... pl.format("{}mo", pl.col("add"))
380
- ... ),
381
- ... add_calendar_years=pl.col("dt").dt.offset_by(
382
- ... pl.format("{}y", pl.col("add"))
383
- ... ),
384
- ... )
385
- shape: (2, 3)
386
- ┌─────────────────────┬─────────────────────┬─────────────────────┐
387
- │ add_calendar_days ┆ add_calendar_months ┆ add_calendar_years │
388
- │ --- ┆ --- ┆ --- │
389
- │ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] │
390
- ╞═════════════════════╪═════════════════════╪═════════════════════╡
391
- │ 2022-01-02 00:00:00 ┆ 2022-02-01 00:00:00 ┆ 2023-01-01 00:00:00 │
392
- │ 2022-01-04 00:00:00 ┆ 2022-03-02 00:00:00 ┆ 2024-01-02 00:00:00 │
393
- └─────────────────────┴─────────────────────┴─────────────────────┘
394
- """ # noqa: W505
395
- if nanoseconds is not None and time_unit is None:
396
- time_unit = "ns"
397
-
398
- weeks_expr = parse_into_expression(weeks) if weeks is not None else None
399
- days_expr = parse_into_expression(days) if days is not None else None
400
- hours_expr = parse_into_expression(hours) if hours is not None else None
401
- minutes_expr = parse_into_expression(minutes) if minutes is not None else None
402
- seconds_expr = parse_into_expression(seconds) if seconds is not None else None
403
- milliseconds_expr = (
404
- parse_into_expression(milliseconds) if milliseconds is not None else None
405
- )
406
- microseconds_expr = (
407
- parse_into_expression(microseconds) if microseconds is not None else None
408
- )
409
- nanoseconds_expr = (
410
- parse_into_expression(nanoseconds) if nanoseconds is not None else None
411
- )
412
-
413
- if time_unit is None:
414
- time_unit = "us"
415
-
416
- return wrap_expr(
417
- plr.duration(
418
- weeks_expr,
419
- days_expr,
420
- hours_expr,
421
- minutes_expr,
422
- seconds_expr,
423
- milliseconds_expr,
424
- microseconds_expr,
425
- nanoseconds_expr,
426
- time_unit,
427
- )
428
- )
429
-
430
-
431
- def concat_list(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) -> Expr:
432
- """
433
- Horizontally concatenate columns into a single list column.
434
-
435
- Operates in linear time.
436
-
437
- Parameters
438
- ----------
439
- exprs
440
- Columns to concatenate into a single list column. Accepts expression input.
441
- Strings are parsed as column names, other non-expression inputs are parsed as
442
- literals.
443
- *more_exprs
444
- Additional columns to concatenate into a single list column, specified as
445
- positional arguments.
446
-
447
- Examples
448
- --------
449
- Concatenate two existing list columns. Null values are propagated.
450
-
451
- >>> df = pl.DataFrame({"a": [[1, 2], [3], [4, 5]], "b": [[4], [], None]})
452
- >>> df.with_columns(concat_list=pl.concat_list("a", "b"))
453
- shape: (3, 3)
454
- ┌───────────┬───────────┬─────────────┐
455
- │ a ┆ b ┆ concat_list │
456
- │ --- ┆ --- ┆ --- │
457
- │ list[i64] ┆ list[i64] ┆ list[i64] │
458
- ╞═══════════╪═══════════╪═════════════╡
459
- │ [1, 2] ┆ [4] ┆ [1, 2, 4] │
460
- │ [3] ┆ [] ┆ [3] │
461
- │ [4, 5] ┆ null ┆ null │
462
- └───────────┴───────────┴─────────────┘
463
-
464
- Non-list columns are cast to a list before concatenation. The output data type
465
- is the supertype of the concatenated columns.
466
-
467
- >>> df.select("a", concat_list=pl.concat_list("a", pl.lit("x")))
468
- shape: (3, 2)
469
- ┌───────────┬─────────────────┐
470
- │ a ┆ concat_list │
471
- │ --- ┆ --- │
472
- │ list[i64] ┆ list[str] │
473
- ╞═══════════╪═════════════════╡
474
- │ [1, 2] ┆ ["1", "2", "x"] │
475
- │ [3] ┆ ["3", "x"] │
476
- │ [4, 5] ┆ ["4", "5", "x"] │
477
- └───────────┴─────────────────┘
478
-
479
- Create lagged columns and collect them into a list. This mimics a rolling window.
480
-
481
- >>> df = pl.DataFrame({"A": [1.0, 2.0, 9.0, 2.0, 13.0]})
482
- >>> df = df.select([pl.col("A").shift(i).alias(f"A_lag_{i}") for i in range(3)])
483
- >>> df.select(
484
- ... pl.concat_list([f"A_lag_{i}" for i in range(3)][::-1]).alias("A_rolling")
485
- ... )
486
- shape: (5, 1)
487
- ┌───────────────────┐
488
- │ A_rolling │
489
- │ --- │
490
- │ list[f64] │
491
- ╞═══════════════════╡
492
- │ [null, null, 1.0] │
493
- │ [null, 1.0, 2.0] │
494
- │ [1.0, 2.0, 9.0] │
495
- │ [2.0, 9.0, 2.0] │
496
- │ [9.0, 2.0, 13.0] │
497
- └───────────────────┘
498
- """
499
- exprs = parse_into_list_of_expressions(exprs, *more_exprs)
500
- return wrap_expr(plr.concat_list(exprs))
501
-
502
-
503
- def concat_arr(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) -> Expr:
504
- """
505
- Horizontally concatenate columns into a single array column.
506
-
507
- Non-array columns are reshaped to a unit-width array. All columns must have
508
- a dtype of either `pl.Array(<DataType>, width)` or `pl.<DataType>`.
509
-
510
- .. warning::
511
- This functionality is considered **unstable**. It may be changed
512
- at any point without it being considered a breaking change.
513
-
514
- Parameters
515
- ----------
516
- exprs
517
- Columns to concatenate into a single array column. Accepts expression input.
518
- Strings are parsed as column names, other non-expression inputs are parsed as
519
- literals.
520
- *more_exprs
521
- Additional columns to concatenate into a single array column, specified as
522
- positional arguments.
523
-
524
- Examples
525
- --------
526
- Concatenate 2 array columns:
527
-
528
- >>> (
529
- ... pl.select(
530
- ... a=pl.Series([[1], [3], None], dtype=pl.Array(pl.Int64, 1)),
531
- ... b=pl.Series([[3], [None], [5]], dtype=pl.Array(pl.Int64, 1)),
532
- ... ).with_columns(
533
- ... pl.concat_arr("a", "b").alias("concat_arr(a, b)"),
534
- ... pl.concat_arr("a", pl.first("b")).alias("concat_arr(a, first(b))"),
535
- ... )
536
- ... )
537
- shape: (3, 4)
538
- ┌───────────────┬───────────────┬──────────────────┬─────────────────────────┐
539
- │ a ┆ b ┆ concat_arr(a, b) ┆ concat_arr(a, first(b)) │
540
- │ --- ┆ --- ┆ --- ┆ --- │
541
- │ array[i64, 1] ┆ array[i64, 1] ┆ array[i64, 2] ┆ array[i64, 2] │
542
- ╞═══════════════╪═══════════════╪══════════════════╪═════════════════════════╡
543
- │ [1] ┆ [3] ┆ [1, 3] ┆ [1, 3] │
544
- │ [3] ┆ [null] ┆ [3, null] ┆ [3, 3] │
545
- │ null ┆ [5] ┆ null ┆ null │
546
- └───────────────┴───────────────┴──────────────────┴─────────────────────────┘
547
-
548
- Concatenate non-array columns:
549
-
550
- >>> (
551
- ... pl.select(
552
- ... c=pl.Series([None, 5, 6], dtype=pl.Int64),
553
- ... )
554
- ... .with_columns(d=pl.col("c").reverse())
555
- ... .with_columns(
556
- ... pl.concat_arr("c", "d").alias("concat_arr(c, d)"),
557
- ... )
558
- ... )
559
- shape: (3, 3)
560
- ┌──────┬──────┬──────────────────┐
561
- │ c ┆ d ┆ concat_arr(c, d) │
562
- │ --- ┆ --- ┆ --- │
563
- │ i64 ┆ i64 ┆ array[i64, 2] │
564
- ╞══════╪══════╪══════════════════╡
565
- │ null ┆ 6 ┆ [null, 6] │
566
- │ 5 ┆ 5 ┆ [5, 5] │
567
- │ 6 ┆ null ┆ [6, null] │
568
- └──────┴──────┴──────────────────┘
569
-
570
- Concatenate mixed array and non-array columns:
571
-
572
- >>> (
573
- ... pl.select(
574
- ... a=pl.Series([[1], [3], None], dtype=pl.Array(pl.Int64, 1)),
575
- ... b=pl.Series([[3], [None], [5]], dtype=pl.Array(pl.Int64, 1)),
576
- ... c=pl.Series([None, 5, 6], dtype=pl.Int64),
577
- ... ).with_columns(
578
- ... pl.concat_arr("a", "b", "c").alias("concat_arr(a, b, c)"),
579
- ... )
580
- ... )
581
- shape: (3, 4)
582
- ┌───────────────┬───────────────┬──────┬─────────────────────┐
583
- │ a ┆ b ┆ c ┆ concat_arr(a, b, c) │
584
- │ --- ┆ --- ┆ --- ┆ --- │
585
- │ array[i64, 1] ┆ array[i64, 1] ┆ i64 ┆ array[i64, 3] │
586
- ╞═══════════════╪═══════════════╪══════╪═════════════════════╡
587
- │ [1] ┆ [3] ┆ null ┆ [1, 3, null] │
588
- │ [3] ┆ [null] ┆ 5 ┆ [3, null, 5] │
589
- │ null ┆ [5] ┆ 6 ┆ null │
590
- └───────────────┴───────────────┴──────┴─────────────────────┘
591
-
592
- Unit-length columns are broadcasted:
593
-
594
- >>> (
595
- ... pl.select(
596
- ... a=pl.Series([1, 3, None]),
597
- ... ).with_columns(
598
- ... pl.concat_arr("a", pl.lit(0, dtype=pl.Int64)).alias("concat_arr(a, 0)"),
599
- ... pl.concat_arr("a", pl.sum("a")).alias("concat_arr(a, sum(a))"),
600
- ... pl.concat_arr("a", pl.max("a")).alias("concat_arr(a, max(a))"),
601
- ... )
602
- ... )
603
- shape: (3, 4)
604
- ┌──────┬──────────────────┬───────────────────────┬───────────────────────┐
605
- │ a ┆ concat_arr(a, 0) ┆ concat_arr(a, sum(a)) ┆ concat_arr(a, max(a)) │
606
- │ --- ┆ --- ┆ --- ┆ --- │
607
- │ i64 ┆ array[i64, 2] ┆ array[i64, 2] ┆ array[i64, 2] │
608
- ╞══════╪══════════════════╪═══════════════════════╪═══════════════════════╡
609
- │ 1 ┆ [1, 0] ┆ [1, 4] ┆ [1, 3] │
610
- │ 3 ┆ [3, 0] ┆ [3, 4] ┆ [3, 3] │
611
- │ null ┆ [null, 0] ┆ [null, 4] ┆ [null, 3] │
612
- └──────┴──────────────────┴───────────────────────┴───────────────────────┘
613
- """
614
- msg = "`concat_arr` functionality is considered unstable"
615
- issue_unstable_warning(msg)
616
-
617
- exprs = parse_into_list_of_expressions(exprs, *more_exprs)
618
- return wrap_expr(plr.concat_arr(exprs))
619
-
620
-
621
- @overload
622
- def struct(
623
- *exprs: IntoExpr | Iterable[IntoExpr],
624
- schema: SchemaDict | None = ...,
625
- eager: Literal[False] = ...,
626
- **named_exprs: IntoExpr,
627
- ) -> Expr: ...
628
-
629
-
630
- @overload
631
- def struct(
632
- *exprs: IntoExpr | Iterable[IntoExpr],
633
- schema: SchemaDict | None = ...,
634
- eager: Literal[True],
635
- **named_exprs: IntoExpr,
636
- ) -> Series: ...
637
-
638
-
639
- @overload
640
- def struct(
641
- *exprs: IntoExpr | Iterable[IntoExpr],
642
- schema: SchemaDict | None = ...,
643
- eager: bool,
644
- **named_exprs: IntoExpr,
645
- ) -> Expr | Series: ...
646
-
647
-
648
- def struct(
649
- *exprs: IntoExpr | Iterable[IntoExpr],
650
- schema: SchemaDict | None = None,
651
- eager: bool = False,
652
- **named_exprs: IntoExpr,
653
- ) -> Expr | Series:
654
- """
655
- Collect columns into a struct column.
656
-
657
- Parameters
658
- ----------
659
- *exprs
660
- Column(s) to collect into a struct column, specified as positional arguments.
661
- Accepts expression input. Strings are parsed as column names,
662
- other non-expression inputs are parsed as literals.
663
- schema
664
- Optional schema that explicitly defines the struct field dtypes. If no columns
665
- or expressions are provided, schema keys are used to define columns.
666
- eager
667
- Evaluate immediately and return a `Series`. If set to `False` (default),
668
- return an expression instead.
669
- **named_exprs
670
- Additional columns to collect into the struct column, specified as keyword
671
- arguments. The columns will be renamed to the keyword used.
672
-
673
- Examples
674
- --------
675
- Collect all columns of a dataframe into a struct by passing `pl.all()`.
676
-
677
- >>> df = pl.DataFrame(
678
- ... {
679
- ... "int": [1, 2],
680
- ... "str": ["a", "b"],
681
- ... "bool": [True, None],
682
- ... "list": [[1, 2], [3]],
683
- ... }
684
- ... )
685
- >>> df.select(pl.struct(pl.all()).alias("my_struct"))
686
- shape: (2, 1)
687
- ┌─────────────────────┐
688
- │ my_struct │
689
- │ --- │
690
- │ struct[4] │
691
- ╞═════════════════════╡
692
- │ {1,"a",true,[1, 2]} │
693
- │ {2,"b",null,[3]} │
694
- └─────────────────────┘
695
-
696
- Collect selected columns into a struct by either passing a list of columns, or by
697
- specifying each column as a positional argument.
698
-
699
- >>> df.select(pl.struct("int", False).alias("my_struct"))
700
- shape: (2, 1)
701
- ┌───────────┐
702
- │ my_struct │
703
- │ --- │
704
- │ struct[2] │
705
- ╞═══════════╡
706
- │ {1,false} │
707
- │ {2,false} │
708
- └───────────┘
709
-
710
- Use keyword arguments to easily name each struct field.
711
-
712
- >>> df.select(pl.struct(p="int", q="bool").alias("my_struct")).schema
713
- Schema({'my_struct': Struct({'p': Int64, 'q': Boolean})})
714
- """
715
- pyexprs = parse_into_list_of_expressions(*exprs, **named_exprs)
716
-
717
- if schema:
718
- if not exprs and not named_exprs:
719
- # no columns or expressions provided; create one from schema keys
720
- expr = wrap_expr(
721
- plr.as_struct(parse_into_list_of_expressions(list(schema.keys())))
722
- )
723
- else:
724
- expr = wrap_expr(plr.as_struct(pyexprs))
725
- expr = expr.cast(Struct(schema), strict=False)
726
- else:
727
- expr = wrap_expr(plr.as_struct(pyexprs))
728
-
729
- if eager:
730
- return F.select(expr).to_series()
731
- else:
732
- return expr
733
-
734
-
735
- def concat_str(
736
- exprs: IntoExpr | Iterable[IntoExpr],
737
- *more_exprs: IntoExpr,
738
- separator: str = "",
739
- ignore_nulls: bool = False,
740
- ) -> Expr:
741
- """
742
- Horizontally concatenate columns into a single string column.
743
-
744
- Operates in linear time.
745
-
746
- Parameters
747
- ----------
748
- exprs
749
- Columns to concatenate into a single string column. Accepts expression input.
750
- Strings are parsed as column names, other non-expression inputs are parsed as
751
- literals. Non-`String` columns are cast to `String`.
752
- *more_exprs
753
- Additional columns to concatenate into a single string column, specified as
754
- positional arguments.
755
- separator
756
- String that will be used to separate the values of each column.
757
- ignore_nulls
758
- Ignore null values (default is ``False``).
759
-
760
- If set to ``False``, null values will be propagated.
761
- if the row contains any null values, the output is null.
762
-
763
- Examples
764
- --------
765
- >>> df = pl.DataFrame(
766
- ... {
767
- ... "a": [1, 2, 3],
768
- ... "b": ["dogs", "cats", None],
769
- ... "c": ["play", "swim", "walk"],
770
- ... }
771
- ... )
772
- >>> df.with_columns(
773
- ... pl.concat_str(
774
- ... [
775
- ... pl.col("a") * 2,
776
- ... pl.col("b"),
777
- ... pl.col("c"),
778
- ... ],
779
- ... separator=" ",
780
- ... ).alias("full_sentence"),
781
- ... )
782
- shape: (3, 4)
783
- ┌─────┬──────┬──────┬───────────────┐
784
- │ a ┆ b ┆ c ┆ full_sentence │
785
- │ --- ┆ --- ┆ --- ┆ --- │
786
- │ i64 ┆ str ┆ str ┆ str │
787
- ╞═════╪══════╪══════╪═══════════════╡
788
- │ 1 ┆ dogs ┆ play ┆ 2 dogs play │
789
- │ 2 ┆ cats ┆ swim ┆ 4 cats swim │
790
- │ 3 ┆ null ┆ walk ┆ null │
791
- └─────┴──────┴──────┴───────────────┘
792
- """
793
- exprs = parse_into_list_of_expressions(exprs, *more_exprs)
794
- return wrap_expr(plr.concat_str(exprs, separator, ignore_nulls))
795
-
796
-
797
- def format(f_string: str, *args: Expr | str) -> Expr:
798
- """
799
- Format expressions as a string.
800
-
801
- Parameters
802
- ----------
803
- f_string
804
- A string that with placeholders.
805
- For example: "hello_{}" or "{}_world
806
- args
807
- Expression(s) that fill the placeholders
808
-
809
- Examples
810
- --------
811
- >>> df = pl.DataFrame(
812
- ... {
813
- ... "a": ["a", "b", "c"],
814
- ... "b": [1, 2, 3],
815
- ... }
816
- ... )
817
- >>> df.select(
818
- ... [
819
- ... pl.format("foo_{}_bar_{}", pl.col("a"), "b").alias("fmt"),
820
- ... ]
821
- ... )
822
- shape: (3, 1)
823
- ┌─────────────┐
824
- │ fmt │
825
- │ --- │
826
- │ str │
827
- ╞═════════════╡
828
- │ foo_a_bar_1 │
829
- │ foo_b_bar_2 │
830
- │ foo_c_bar_3 │
831
- └─────────────┘
832
- """
833
- if f_string.count("{}") != len(args):
834
- msg = "number of placeholders should equal the number of arguments"
835
- raise ValueError(msg)
836
-
837
- exprs = []
838
-
839
- arguments = iter(args)
840
- for i, s in enumerate(f_string.split("{}")):
841
- if i > 0:
842
- e = wrap_expr(parse_into_expression(next(arguments)))
843
- exprs.append(e)
844
-
845
- if len(s) > 0:
846
- exprs.append(F.lit(s))
847
-
848
- return concat_str(exprs, separator="")