polars-runtime-compat 1.34.0b2__cp39-abi3-manylinux_2_24_aarch64.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/.gitkeep +0 -0
  2. _polars_runtime_compat/_polars_runtime_compat.abi3.so +0 -0
  3. polars/__init__.py +528 -0
  4. polars/_cpu_check.py +265 -0
  5. polars/_dependencies.py +355 -0
  6. polars/_plr.py +99 -0
  7. polars/_plr.pyi +2496 -0
  8. polars/_reexport.py +23 -0
  9. polars/_typing.py +478 -0
  10. polars/_utils/__init__.py +37 -0
  11. polars/_utils/async_.py +102 -0
  12. polars/_utils/cache.py +176 -0
  13. polars/_utils/cloud.py +40 -0
  14. polars/_utils/constants.py +29 -0
  15. polars/_utils/construction/__init__.py +46 -0
  16. polars/_utils/construction/dataframe.py +1397 -0
  17. polars/_utils/construction/other.py +72 -0
  18. polars/_utils/construction/series.py +560 -0
  19. polars/_utils/construction/utils.py +118 -0
  20. polars/_utils/convert.py +224 -0
  21. polars/_utils/deprecation.py +406 -0
  22. polars/_utils/getitem.py +457 -0
  23. polars/_utils/logging.py +11 -0
  24. polars/_utils/nest_asyncio.py +264 -0
  25. polars/_utils/parquet.py +15 -0
  26. polars/_utils/parse/__init__.py +12 -0
  27. polars/_utils/parse/expr.py +242 -0
  28. polars/_utils/polars_version.py +19 -0
  29. polars/_utils/pycapsule.py +53 -0
  30. polars/_utils/scan.py +27 -0
  31. polars/_utils/serde.py +63 -0
  32. polars/_utils/slice.py +215 -0
  33. polars/_utils/udfs.py +1251 -0
  34. polars/_utils/unstable.py +63 -0
  35. polars/_utils/various.py +782 -0
  36. polars/_utils/wrap.py +25 -0
  37. polars/api.py +370 -0
  38. polars/catalog/__init__.py +0 -0
  39. polars/catalog/unity/__init__.py +19 -0
  40. polars/catalog/unity/client.py +733 -0
  41. polars/catalog/unity/models.py +152 -0
  42. polars/config.py +1571 -0
  43. polars/convert/__init__.py +25 -0
  44. polars/convert/general.py +1046 -0
  45. polars/convert/normalize.py +261 -0
  46. polars/dataframe/__init__.py +5 -0
  47. polars/dataframe/_html.py +186 -0
  48. polars/dataframe/frame.py +12582 -0
  49. polars/dataframe/group_by.py +1067 -0
  50. polars/dataframe/plotting.py +257 -0
  51. polars/datatype_expr/__init__.py +5 -0
  52. polars/datatype_expr/array.py +56 -0
  53. polars/datatype_expr/datatype_expr.py +304 -0
  54. polars/datatype_expr/list.py +18 -0
  55. polars/datatype_expr/struct.py +69 -0
  56. polars/datatypes/__init__.py +122 -0
  57. polars/datatypes/_parse.py +195 -0
  58. polars/datatypes/_utils.py +48 -0
  59. polars/datatypes/classes.py +1213 -0
  60. polars/datatypes/constants.py +11 -0
  61. polars/datatypes/constructor.py +172 -0
  62. polars/datatypes/convert.py +366 -0
  63. polars/datatypes/group.py +130 -0
  64. polars/exceptions.py +230 -0
  65. polars/expr/__init__.py +7 -0
  66. polars/expr/array.py +964 -0
  67. polars/expr/binary.py +346 -0
  68. polars/expr/categorical.py +306 -0
  69. polars/expr/datetime.py +2620 -0
  70. polars/expr/expr.py +11272 -0
  71. polars/expr/list.py +1408 -0
  72. polars/expr/meta.py +444 -0
  73. polars/expr/name.py +321 -0
  74. polars/expr/string.py +3045 -0
  75. polars/expr/struct.py +357 -0
  76. polars/expr/whenthen.py +185 -0
  77. polars/functions/__init__.py +193 -0
  78. polars/functions/aggregation/__init__.py +33 -0
  79. polars/functions/aggregation/horizontal.py +298 -0
  80. polars/functions/aggregation/vertical.py +341 -0
  81. polars/functions/as_datatype.py +848 -0
  82. polars/functions/business.py +138 -0
  83. polars/functions/col.py +384 -0
  84. polars/functions/datatype.py +121 -0
  85. polars/functions/eager.py +524 -0
  86. polars/functions/escape_regex.py +29 -0
  87. polars/functions/lazy.py +2751 -0
  88. polars/functions/len.py +68 -0
  89. polars/functions/lit.py +210 -0
  90. polars/functions/random.py +22 -0
  91. polars/functions/range/__init__.py +19 -0
  92. polars/functions/range/_utils.py +15 -0
  93. polars/functions/range/date_range.py +303 -0
  94. polars/functions/range/datetime_range.py +370 -0
  95. polars/functions/range/int_range.py +348 -0
  96. polars/functions/range/linear_space.py +311 -0
  97. polars/functions/range/time_range.py +287 -0
  98. polars/functions/repeat.py +301 -0
  99. polars/functions/whenthen.py +353 -0
  100. polars/interchange/__init__.py +10 -0
  101. polars/interchange/buffer.py +77 -0
  102. polars/interchange/column.py +190 -0
  103. polars/interchange/dataframe.py +230 -0
  104. polars/interchange/from_dataframe.py +328 -0
  105. polars/interchange/protocol.py +303 -0
  106. polars/interchange/utils.py +170 -0
  107. polars/io/__init__.py +64 -0
  108. polars/io/_utils.py +317 -0
  109. polars/io/avro.py +49 -0
  110. polars/io/clipboard.py +36 -0
  111. polars/io/cloud/__init__.py +17 -0
  112. polars/io/cloud/_utils.py +80 -0
  113. polars/io/cloud/credential_provider/__init__.py +17 -0
  114. polars/io/cloud/credential_provider/_builder.py +520 -0
  115. polars/io/cloud/credential_provider/_providers.py +618 -0
  116. polars/io/csv/__init__.py +9 -0
  117. polars/io/csv/_utils.py +38 -0
  118. polars/io/csv/batched_reader.py +142 -0
  119. polars/io/csv/functions.py +1495 -0
  120. polars/io/database/__init__.py +6 -0
  121. polars/io/database/_arrow_registry.py +70 -0
  122. polars/io/database/_cursor_proxies.py +147 -0
  123. polars/io/database/_executor.py +578 -0
  124. polars/io/database/_inference.py +314 -0
  125. polars/io/database/_utils.py +144 -0
  126. polars/io/database/functions.py +516 -0
  127. polars/io/delta.py +499 -0
  128. polars/io/iceberg/__init__.py +3 -0
  129. polars/io/iceberg/_utils.py +697 -0
  130. polars/io/iceberg/dataset.py +556 -0
  131. polars/io/iceberg/functions.py +151 -0
  132. polars/io/ipc/__init__.py +8 -0
  133. polars/io/ipc/functions.py +514 -0
  134. polars/io/json/__init__.py +3 -0
  135. polars/io/json/read.py +101 -0
  136. polars/io/ndjson.py +332 -0
  137. polars/io/parquet/__init__.py +17 -0
  138. polars/io/parquet/field_overwrites.py +140 -0
  139. polars/io/parquet/functions.py +722 -0
  140. polars/io/partition.py +491 -0
  141. polars/io/plugins.py +187 -0
  142. polars/io/pyarrow_dataset/__init__.py +5 -0
  143. polars/io/pyarrow_dataset/anonymous_scan.py +109 -0
  144. polars/io/pyarrow_dataset/functions.py +79 -0
  145. polars/io/scan_options/__init__.py +5 -0
  146. polars/io/scan_options/_options.py +59 -0
  147. polars/io/scan_options/cast_options.py +126 -0
  148. polars/io/spreadsheet/__init__.py +6 -0
  149. polars/io/spreadsheet/_utils.py +52 -0
  150. polars/io/spreadsheet/_write_utils.py +647 -0
  151. polars/io/spreadsheet/functions.py +1323 -0
  152. polars/lazyframe/__init__.py +9 -0
  153. polars/lazyframe/engine_config.py +61 -0
  154. polars/lazyframe/frame.py +8564 -0
  155. polars/lazyframe/group_by.py +669 -0
  156. polars/lazyframe/in_process.py +42 -0
  157. polars/lazyframe/opt_flags.py +333 -0
  158. polars/meta/__init__.py +14 -0
  159. polars/meta/build.py +33 -0
  160. polars/meta/index_type.py +27 -0
  161. polars/meta/thread_pool.py +50 -0
  162. polars/meta/versions.py +120 -0
  163. polars/ml/__init__.py +0 -0
  164. polars/ml/torch.py +213 -0
  165. polars/ml/utilities.py +30 -0
  166. polars/plugins.py +155 -0
  167. polars/py.typed +0 -0
  168. polars/pyproject.toml +96 -0
  169. polars/schema.py +265 -0
  170. polars/selectors.py +3117 -0
  171. polars/series/__init__.py +5 -0
  172. polars/series/array.py +776 -0
  173. polars/series/binary.py +254 -0
  174. polars/series/categorical.py +246 -0
  175. polars/series/datetime.py +2275 -0
  176. polars/series/list.py +1087 -0
  177. polars/series/plotting.py +191 -0
  178. polars/series/series.py +9197 -0
  179. polars/series/string.py +2367 -0
  180. polars/series/struct.py +154 -0
  181. polars/series/utils.py +191 -0
  182. polars/sql/__init__.py +7 -0
  183. polars/sql/context.py +677 -0
  184. polars/sql/functions.py +139 -0
  185. polars/string_cache.py +185 -0
  186. polars/testing/__init__.py +13 -0
  187. polars/testing/asserts/__init__.py +9 -0
  188. polars/testing/asserts/frame.py +231 -0
  189. polars/testing/asserts/series.py +219 -0
  190. polars/testing/asserts/utils.py +12 -0
  191. polars/testing/parametric/__init__.py +33 -0
  192. polars/testing/parametric/profiles.py +107 -0
  193. polars/testing/parametric/strategies/__init__.py +22 -0
  194. polars/testing/parametric/strategies/_utils.py +14 -0
  195. polars/testing/parametric/strategies/core.py +615 -0
  196. polars/testing/parametric/strategies/data.py +452 -0
  197. polars/testing/parametric/strategies/dtype.py +436 -0
  198. polars/testing/parametric/strategies/legacy.py +169 -0
  199. polars/type_aliases.py +24 -0
  200. polars_runtime_compat-1.34.0b2.dist-info/METADATA +190 -0
  201. polars_runtime_compat-1.34.0b2.dist-info/RECORD +203 -0
  202. polars_runtime_compat-1.34.0b2.dist-info/WHEEL +4 -0
  203. polars_runtime_compat-1.34.0b2.dist-info/licenses/LICENSE +20 -0
@@ -0,0 +1,301 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ from decimal import Decimal as D
5
+ from functools import lru_cache
6
+ from typing import TYPE_CHECKING, Any, overload
7
+
8
+ from polars import functions as F
9
+ from polars._utils.parse import parse_into_expression
10
+ from polars._utils.various import qualified_type_name
11
+ from polars._utils.wrap import wrap_expr
12
+ from polars.datatypes import (
13
+ Array,
14
+ Boolean,
15
+ Decimal,
16
+ Float64,
17
+ List,
18
+ Utf8,
19
+ )
20
+ from polars.datatypes.group import FLOAT_DTYPES, INTEGER_DTYPES
21
+
22
+ with contextlib.suppress(ImportError): # Module not available when building docs
23
+ import polars._plr as plr
24
+
25
+
26
+ if TYPE_CHECKING:
27
+ from typing import Literal
28
+
29
+ from polars import Expr, Series
30
+ from polars._typing import IntoExpr, PolarsDataType
31
+
32
+
33
+ # create a lookup of dtypes that have a reasonable one/zero mapping; for
34
+ # anything more elaborate should use `repeat`
35
+ @lru_cache(16)
36
+ def _one_or_zero_by_dtype(value: int, dtype: PolarsDataType) -> Any:
37
+ if dtype in INTEGER_DTYPES:
38
+ return value
39
+ elif dtype in FLOAT_DTYPES:
40
+ return float(value)
41
+ elif dtype == Boolean:
42
+ return bool(value)
43
+ elif dtype == Utf8:
44
+ return str(value)
45
+ elif isinstance(dtype, Decimal):
46
+ return D(value)
47
+ elif isinstance(dtype, (List, Array)):
48
+ arr_width = getattr(dtype, "size", 1)
49
+ return [_one_or_zero_by_dtype(value, dtype.inner)] * arr_width
50
+ return None
51
+
52
+
53
+ @overload
54
+ def repeat(
55
+ value: IntoExpr | None,
56
+ n: int | Expr,
57
+ *,
58
+ dtype: PolarsDataType | None = ...,
59
+ eager: Literal[False] = ...,
60
+ ) -> Expr: ...
61
+
62
+
63
+ @overload
64
+ def repeat(
65
+ value: IntoExpr | None,
66
+ n: int | Expr,
67
+ *,
68
+ dtype: PolarsDataType | None = ...,
69
+ eager: Literal[True],
70
+ ) -> Series: ...
71
+
72
+
73
+ @overload
74
+ def repeat(
75
+ value: IntoExpr | None,
76
+ n: int | Expr,
77
+ *,
78
+ dtype: PolarsDataType | None = ...,
79
+ eager: bool,
80
+ ) -> Expr | Series: ...
81
+
82
+
83
+ def repeat(
84
+ value: IntoExpr | None,
85
+ n: int | Expr,
86
+ *,
87
+ dtype: PolarsDataType | None = None,
88
+ eager: bool = False,
89
+ ) -> Expr | Series:
90
+ """
91
+ Construct a column of length `n` filled with the given value.
92
+
93
+ Parameters
94
+ ----------
95
+ value
96
+ Value to repeat.
97
+ n
98
+ Length of the resulting column.
99
+ dtype
100
+ Data type of the resulting column. If set to `None` (default), data type is
101
+ inferred from the given value. Defaults to Int32 for integer values, unless
102
+ Int64 is required to fit the given value. Defaults to Float64 for float values.
103
+ eager
104
+ Evaluate immediately and return a `Series`. If set to `False` (default),
105
+ return an expression instead.
106
+
107
+ Notes
108
+ -----
109
+ If you want to construct a column in lazy mode and do not need a pre-determined
110
+ length, use :func:`lit` instead.
111
+
112
+ See Also
113
+ --------
114
+ lit
115
+
116
+ Examples
117
+ --------
118
+ Construct a column with a repeated value in a lazy context.
119
+
120
+ >>> pl.select(pl.repeat("z", n=3)).to_series()
121
+ shape: (3,)
122
+ Series: 'repeat' [str]
123
+ [
124
+ "z"
125
+ "z"
126
+ "z"
127
+ ]
128
+
129
+ Generate a Series directly by setting `eager=True`.
130
+
131
+ >>> pl.repeat(3, n=3, dtype=pl.Int8, eager=True)
132
+ shape: (3,)
133
+ Series: 'repeat' [i8]
134
+ [
135
+ 3
136
+ 3
137
+ 3
138
+ ]
139
+ """
140
+ if isinstance(n, int):
141
+ n = F.lit(n)
142
+ if not hasattr(n, "_pyexpr"):
143
+ msg = f"`n` parameter of `repeat expected a `int` or `Expr` got a `{qualified_type_name(n)}`"
144
+ raise TypeError(msg)
145
+ value_pyexpr = parse_into_expression(value, str_as_lit=True, dtype=dtype)
146
+ expr = wrap_expr(plr.repeat(value_pyexpr, n._pyexpr, dtype))
147
+ if eager:
148
+ return F.select(expr).to_series()
149
+ return expr
150
+
151
+
152
+ @overload
153
+ def ones(
154
+ n: int | Expr,
155
+ dtype: PolarsDataType = ...,
156
+ *,
157
+ eager: Literal[False] = ...,
158
+ ) -> Expr: ...
159
+
160
+
161
+ @overload
162
+ def ones(
163
+ n: int | Expr,
164
+ dtype: PolarsDataType = ...,
165
+ *,
166
+ eager: Literal[True],
167
+ ) -> Series: ...
168
+
169
+
170
+ @overload
171
+ def ones(
172
+ n: int | Expr,
173
+ dtype: PolarsDataType = ...,
174
+ *,
175
+ eager: bool,
176
+ ) -> Expr | Series: ...
177
+
178
+
179
+ def ones(
180
+ n: int | Expr,
181
+ dtype: PolarsDataType = Float64,
182
+ *,
183
+ eager: bool = False,
184
+ ) -> Expr | Series:
185
+ """
186
+ Construct a column of length `n` filled with ones.
187
+
188
+ This is syntactic sugar for the `repeat` function.
189
+
190
+ Parameters
191
+ ----------
192
+ n
193
+ Length of the resulting column.
194
+ dtype
195
+ Data type of the resulting column. Defaults to Float64.
196
+ eager
197
+ Evaluate immediately and return a `Series`. If set to `False`,
198
+ return an expression instead.
199
+
200
+ Notes
201
+ -----
202
+ If you want to construct a column in lazy mode and do not need a pre-determined
203
+ length, use :func:`lit` instead.
204
+
205
+ See Also
206
+ --------
207
+ repeat
208
+ lit
209
+
210
+ Examples
211
+ --------
212
+ >>> pl.ones(3, pl.Int8, eager=True)
213
+ shape: (3,)
214
+ Series: 'ones' [i8]
215
+ [
216
+ 1
217
+ 1
218
+ 1
219
+ ]
220
+ """
221
+ if (one := _one_or_zero_by_dtype(1, dtype)) is None:
222
+ msg = f"invalid dtype for `ones`; found {dtype}"
223
+ raise TypeError(msg)
224
+
225
+ return repeat(one, n=n, dtype=dtype, eager=eager).alias("ones")
226
+
227
+
228
+ @overload
229
+ def zeros(
230
+ n: int | Expr,
231
+ dtype: PolarsDataType = ...,
232
+ *,
233
+ eager: Literal[False] = ...,
234
+ ) -> Expr: ...
235
+
236
+
237
+ @overload
238
+ def zeros(
239
+ n: int | Expr,
240
+ dtype: PolarsDataType = ...,
241
+ *,
242
+ eager: Literal[True],
243
+ ) -> Series: ...
244
+
245
+
246
+ @overload
247
+ def zeros(
248
+ n: int | Expr,
249
+ dtype: PolarsDataType = ...,
250
+ *,
251
+ eager: bool,
252
+ ) -> Expr | Series: ...
253
+
254
+
255
+ def zeros(
256
+ n: int | Expr,
257
+ dtype: PolarsDataType = Float64,
258
+ *,
259
+ eager: bool = False,
260
+ ) -> Expr | Series:
261
+ """
262
+ Construct a column of length `n` filled with zeros.
263
+
264
+ This is syntactic sugar for the `repeat` function.
265
+
266
+ Parameters
267
+ ----------
268
+ n
269
+ Length of the resulting column.
270
+ dtype
271
+ Data type of the resulting column. Defaults to Float64.
272
+ eager
273
+ Evaluate immediately and return a `Series`. If set to `False`,
274
+ return an expression instead.
275
+
276
+ Notes
277
+ -----
278
+ If you want to construct a column in lazy mode and do not need a pre-determined
279
+ length, use :func:`lit` instead.
280
+
281
+ See Also
282
+ --------
283
+ repeat
284
+ lit
285
+
286
+ Examples
287
+ --------
288
+ >>> pl.zeros(3, pl.Int8, eager=True)
289
+ shape: (3,)
290
+ Series: 'zeros' [i8]
291
+ [
292
+ 0
293
+ 0
294
+ 0
295
+ ]
296
+ """
297
+ if (zero := _one_or_zero_by_dtype(0, dtype)) is None:
298
+ msg = f"invalid dtype for `zeros`; found {dtype}"
299
+ raise TypeError(msg)
300
+
301
+ return repeat(zero, n=n, dtype=dtype, eager=eager).alias("zeros")
@@ -0,0 +1,353 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ from typing import TYPE_CHECKING, Any
5
+
6
+ import polars._reexport as pl
7
+ from polars._utils.parse import parse_predicates_constraints_into_expression
8
+
9
+ with contextlib.suppress(ImportError): # Module not available when building docs
10
+ import polars._plr as plr
11
+
12
+ if TYPE_CHECKING:
13
+ from collections.abc import Iterable
14
+
15
+ from polars._typing import IntoExprColumn
16
+
17
+
18
+ def when(
19
+ *predicates: IntoExprColumn | Iterable[IntoExprColumn] | bool,
20
+ **constraints: Any,
21
+ ) -> pl.When:
22
+ """
23
+ Start a `when-then-otherwise` expression.
24
+
25
+ Always initiated by a `pl.when().then()`., and optionally followed by chaining one
26
+ or more `.when().then()` statements.
27
+
28
+ An optional `.otherwise()` can be appended at the end. If not declared, a default
29
+ of `.otherwise(None)` is used.
30
+
31
+ Similar to :func:`coalesce`, the value from the first condition that
32
+ evaluates to True will be picked.
33
+
34
+ If all conditions are False, the `otherwise` value is picked.
35
+
36
+ Parameters
37
+ ----------
38
+ predicates
39
+ Condition(s) that must be met in order to apply the subsequent statement.
40
+ Accepts one or more boolean expressions, which are implicitly combined with
41
+ `&`.
42
+ constraints
43
+ Apply conditions as `col_name = value` keyword arguments that are treated as
44
+ equality matches, such as `x = 123`. As with the predicates parameter, multiple
45
+ conditions are implicitly combined using `&`.
46
+
47
+ Warnings
48
+ --------
49
+ Polars computes all expressions passed to `when-then-otherwise` in parallel and
50
+ filters afterwards. This means each expression must be valid on its own, regardless
51
+ of the conditions in the `when-then-otherwise` chain.
52
+
53
+ Notes
54
+ -----
55
+ String inputs e.g. `when("string")`, `then("string")` or `otherwise("string")`
56
+ are parsed as column names. :func:`lit` can be used to create string values.
57
+
58
+ Examples
59
+ --------
60
+ Below we add a column with the value 1, where column "foo" > 2 and the value
61
+ 1 + column "bar" where it isn't.
62
+
63
+ >>> df = pl.DataFrame({"foo": [1, 3, 4], "bar": [3, 4, 0]})
64
+ >>> df.with_columns(
65
+ ... pl.when(pl.col.foo > 2).then(1).otherwise(1 + pl.col.bar).alias("val")
66
+ ... )
67
+ shape: (3, 3)
68
+ ┌─────┬─────┬─────┐
69
+ │ foo ┆ bar ┆ val │
70
+ │ --- ┆ --- ┆ --- │
71
+ │ i64 ┆ i64 ┆ i64 │
72
+ ╞═════╪═════╪═════╡
73
+ │ 1 ┆ 3 ┆ 4 │
74
+ │ 3 ┆ 4 ┆ 1 │
75
+ │ 4 ┆ 0 ┆ 1 │
76
+ └─────┴─────┴─────┘
77
+
78
+ Note that `when-then` always executes all expressions.
79
+
80
+ The results are folded left to right, picking the `then` value from the first `when`
81
+ condition that is True.
82
+
83
+ If no `when` condition is True the `otherwise` value is picked.
84
+
85
+ >>> df.with_columns(
86
+ ... when = pl.col.foo > 2,
87
+ ... then = 1,
88
+ ... otherwise = 1 + pl.col.bar
89
+ ... ).with_columns(
90
+ ... pl.when("when").then("then").otherwise("otherwise").alias("val")
91
+ ... )
92
+ shape: (3, 6)
93
+ ┌─────┬─────┬───────┬──────┬───────────┬─────┐
94
+ │ foo ┆ bar ┆ when ┆ then ┆ otherwise ┆ val │
95
+ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
96
+ │ i64 ┆ i64 ┆ bool ┆ i32 ┆ i64 ┆ i64 │
97
+ ╞═════╪═════╪═══════╪══════╪═══════════╪═════╡
98
+ │ 1 ┆ 3 ┆ false ┆ 1 ┆ 4 ┆ 4 │
99
+ │ 3 ┆ 4 ┆ true ┆ 1 ┆ 5 ┆ 1 │
100
+ │ 4 ┆ 0 ┆ true ┆ 1 ┆ 1 ┆ 1 │
101
+ └─────┴─────┴───────┴──────┴───────────┴─────┘
102
+
103
+ Note that in regular Polars usage, a single string is parsed as a column name.
104
+
105
+ >>> df.with_columns(
106
+ ... when = pl.col.foo > 2,
107
+ ... then = "foo",
108
+ ... otherwise = "bar"
109
+ ... )
110
+ shape: (3, 5)
111
+ ┌─────┬─────┬───────┬──────┬───────────┐
112
+ │ foo ┆ bar ┆ when ┆ then ┆ otherwise │
113
+ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
114
+ │ i64 ┆ i64 ┆ bool ┆ i64 ┆ i64 │
115
+ ╞═════╪═════╪═══════╪══════╪═══════════╡
116
+ │ 1 ┆ 3 ┆ false ┆ 1 ┆ 3 │
117
+ │ 3 ┆ 4 ┆ true ┆ 3 ┆ 4 │
118
+ │ 4 ┆ 0 ┆ true ┆ 4 ┆ 0 │
119
+ └─────┴─────┴───────┴──────┴───────────┘
120
+
121
+ For consistency, `when-then` behaves in the same way.
122
+
123
+ >>> df.with_columns(
124
+ ... pl.when(pl.col.foo > 2).then("foo").otherwise("bar").alias("val")
125
+ ... )
126
+ shape: (3, 3)
127
+ ┌─────┬─────┬─────┐
128
+ │ foo ┆ bar ┆ val │
129
+ │ --- ┆ --- ┆ --- │
130
+ │ i64 ┆ i64 ┆ i64 │
131
+ ╞═════╪═════╪═════╡
132
+ │ 1 ┆ 3 ┆ 3 │
133
+ │ 3 ┆ 4 ┆ 3 │
134
+ │ 4 ┆ 0 ┆ 4 │
135
+ └─────┴─────┴─────┘
136
+
137
+ :func:`lit` can be used to create string values.
138
+
139
+ >>> df.with_columns(
140
+ ... pl.when(pl.col.foo > 2)
141
+ ... .then(pl.lit("foo"))
142
+ ... .otherwise(pl.lit("bar"))
143
+ ... .alias("val")
144
+ ... )
145
+ shape: (3, 3)
146
+ ┌─────┬─────┬─────┐
147
+ │ foo ┆ bar ┆ val │
148
+ │ --- ┆ --- ┆ --- │
149
+ │ i64 ┆ i64 ┆ str │
150
+ ╞═════╪═════╪═════╡
151
+ │ 1 ┆ 3 ┆ bar │
152
+ │ 3 ┆ 4 ┆ foo │
153
+ │ 4 ┆ 0 ┆ foo │
154
+ └─────┴─────┴─────┘
155
+
156
+ Multiple `when-then` statements can be chained.
157
+
158
+ >>> df.with_columns(
159
+ ... pl.when(pl.col.foo > 2)
160
+ ... .then(1)
161
+ ... .when(pl.col.bar > 2)
162
+ ... .then(4)
163
+ ... .otherwise(-1)
164
+ ... .alias("val")
165
+ ... )
166
+ shape: (3, 3)
167
+ ┌─────┬─────┬─────┐
168
+ │ foo ┆ bar ┆ val │
169
+ │ --- ┆ --- ┆ --- │
170
+ │ i64 ┆ i64 ┆ i32 │
171
+ ╞═════╪═════╪═════╡
172
+ │ 1 ┆ 3 ┆ 4 │
173
+ │ 3 ┆ 4 ┆ 1 │
174
+ │ 4 ┆ 0 ┆ 1 │
175
+ └─────┴─────┴─────┘
176
+
177
+ In the case of `foo=3` and `bar=4`, both conditions are True but the first value
178
+ (i.e. 1) is picked.
179
+
180
+ >>> df.with_columns(
181
+ ... when1 = pl.col.foo > 2,
182
+ ... then1 = 1,
183
+ ... when2 = pl.col.bar > 2,
184
+ ... then2 = 4,
185
+ ... otherwise = -1
186
+ ... )
187
+ shape: (3, 7)
188
+ ┌─────┬─────┬───────┬───────┬───────┬───────┬───────────┐
189
+ │ foo ┆ bar ┆ when1 ┆ then1 ┆ when2 ┆ then2 ┆ otherwise │
190
+ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
191
+ │ i64 ┆ i64 ┆ bool ┆ i32 ┆ bool ┆ i32 ┆ i32 │
192
+ ╞═════╪═════╪═══════╪═══════╪═══════╪═══════╪═══════════╡
193
+ │ 1 ┆ 3 ┆ false ┆ 1 ┆ true ┆ 4 ┆ -1 │
194
+ │ 3 ┆ 4 ┆ true ┆ 1 ┆ true ┆ 4 ┆ -1 │
195
+ │ 4 ┆ 0 ┆ true ┆ 1 ┆ false ┆ 4 ┆ -1 │
196
+ └─────┴─────┴───────┴───────┴───────┴───────┴───────────┘
197
+
198
+ The `otherwise` statement is optional and defaults to `.otherwise(None)`
199
+ if not given.
200
+
201
+ This idiom is commonly used to null out values.
202
+
203
+ >>> df.with_columns(pl.when(pl.col.foo == 3).then("bar"))
204
+ shape: (3, 2)
205
+ ┌─────┬──────┐
206
+ │ foo ┆ bar │
207
+ │ --- ┆ --- │
208
+ │ i64 ┆ i64 │
209
+ ╞═════╪══════╡
210
+ │ 1 ┆ null │
211
+ │ 3 ┆ 4 │
212
+ │ 4 ┆ null │
213
+ └─────┴──────┘
214
+
215
+ `when` accepts keyword arguments as shorthand for equality conditions.
216
+
217
+ >>> df.with_columns(pl.when(foo=3).then("bar"))
218
+ shape: (3, 2)
219
+ ┌─────┬──────┐
220
+ │ foo ┆ bar │
221
+ │ --- ┆ --- │
222
+ │ i64 ┆ i64 │
223
+ ╞═════╪══════╡
224
+ │ 1 ┆ null │
225
+ │ 3 ┆ 4 │
226
+ │ 4 ┆ null │
227
+ └─────┴──────┘
228
+
229
+ Multiple predicates passed to `when` are combined with `&`
230
+
231
+ >>> df.with_columns(
232
+ ... pl.when(pl.col.foo > 2, pl.col.bar < 3) # when((pred1) & (pred2))
233
+ ... .then(pl.lit("Yes"))
234
+ ... .otherwise(pl.lit("No"))
235
+ ... .alias("val")
236
+ ... )
237
+ shape: (3, 3)
238
+ ┌─────┬─────┬─────┐
239
+ │ foo ┆ bar ┆ val │
240
+ │ --- ┆ --- ┆ --- │
241
+ │ i64 ┆ i64 ┆ str │
242
+ ╞═════╪═════╪═════╡
243
+ │ 1 ┆ 3 ┆ No │
244
+ │ 3 ┆ 4 ┆ No │
245
+ │ 4 ┆ 0 ┆ Yes │
246
+ └─────┴─────┴─────┘
247
+
248
+ It could also be thought of as an implicit :func:`all_horizontal` being present.
249
+
250
+ >>> df.with_columns(
251
+ ... when = pl.all_horizontal(pl.col.foo > 2, pl.col.bar < 3)
252
+ ... )
253
+ shape: (3, 3)
254
+ ┌─────┬─────┬───────┐
255
+ │ foo ┆ bar ┆ when │
256
+ │ --- ┆ --- ┆ --- │
257
+ │ i64 ┆ i64 ┆ bool │
258
+ ╞═════╪═════╪═══════╡
259
+ │ 1 ┆ 3 ┆ false │
260
+ │ 3 ┆ 4 ┆ false │
261
+ │ 4 ┆ 0 ┆ true │
262
+ └─────┴─────┴───────┘
263
+
264
+ Structs can be used as a way to return multiple values.
265
+
266
+ Here we swap the "foo" and "bar" values when "foo" is greater than 2.
267
+
268
+ >>> df.with_columns(
269
+ ... pl.when(pl.col.foo > 2)
270
+ ... .then(pl.struct(foo="bar", bar="foo"))
271
+ ... .otherwise(pl.struct("foo", "bar"))
272
+ ... .struct.unnest()
273
+ ... )
274
+ shape: (3, 2)
275
+ ┌─────┬─────┐
276
+ │ foo ┆ bar │
277
+ │ --- ┆ --- │
278
+ │ i64 ┆ i64 │
279
+ ╞═════╪═════╡
280
+ │ 1 ┆ 3 │
281
+ │ 4 ┆ 3 │
282
+ │ 0 ┆ 4 │
283
+ └─────┴─────┘
284
+
285
+ The struct fields are given the same name as the target columns, which are then
286
+ unnested.
287
+
288
+ >>> df.with_columns(
289
+ ... when = pl.col.foo > 2,
290
+ ... then = pl.struct(foo="bar", bar="foo"),
291
+ ... otherwise = pl.struct("foo", "bar")
292
+ ... )
293
+ shape: (3, 5)
294
+ ┌─────┬─────┬───────┬───────────┬───────────┐
295
+ │ foo ┆ bar ┆ when ┆ then ┆ otherwise │
296
+ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
297
+ │ i64 ┆ i64 ┆ bool ┆ struct[2] ┆ struct[2] │
298
+ ╞═════╪═════╪═══════╪═══════════╪═══════════╡
299
+ │ 1 ┆ 3 ┆ false ┆ {3,1} ┆ {1,3} │
300
+ │ 3 ┆ 4 ┆ true ┆ {4,3} ┆ {3,4} │
301
+ │ 4 ┆ 0 ┆ true ┆ {0,4} ┆ {4,0} │
302
+ └─────┴─────┴───────┴───────────┴───────────┘
303
+
304
+ The output name of a `when-then` expression comes from the first `then` branch.
305
+
306
+ Here we try to set all columns to 0 if any column contains a value less than 2.
307
+
308
+ >>> df.with_columns( # doctest: +SKIP
309
+ ... pl.when(pl.any_horizontal(pl.all() < 2))
310
+ ... .then(0)
311
+ ... .otherwise(pl.all())
312
+ ... )
313
+ # ComputeError: the name 'literal' passed to `LazyFrame.with_columns` is duplicate
314
+
315
+ :meth:`.name.keep` could be used to give preference to the column expression.
316
+
317
+ >>> df.with_columns(
318
+ ... pl.when(pl.any_horizontal(pl.all() < 2))
319
+ ... .then(0)
320
+ ... .otherwise(pl.all())
321
+ ... .name.keep()
322
+ ... )
323
+ shape: (3, 2)
324
+ ┌─────┬─────┐
325
+ │ foo ┆ bar │
326
+ │ --- ┆ --- │
327
+ │ i64 ┆ i64 │
328
+ ╞═════╪═════╡
329
+ │ 0 ┆ 0 │
330
+ │ 3 ┆ 4 │
331
+ │ 0 ┆ 0 │
332
+ └─────┴─────┘
333
+
334
+ The logic could also be changed to move the column expression inside `then`.
335
+
336
+ >>> df.with_columns(
337
+ ... pl.when(pl.any_horizontal(pl.all() < 2).not_())
338
+ ... .then(pl.all())
339
+ ... .otherwise(0)
340
+ ... )
341
+ shape: (3, 2)
342
+ ┌─────┬─────┐
343
+ │ foo ┆ bar │
344
+ │ --- ┆ --- │
345
+ │ i64 ┆ i64 │
346
+ ╞═════╪═════╡
347
+ │ 0 ┆ 0 │
348
+ │ 3 ┆ 4 │
349
+ │ 0 ┆ 0 │
350
+ └─────┴─────┘
351
+ """ # fmt: skip
352
+ condition = parse_predicates_constraints_into_expression(*predicates, **constraints)
353
+ return pl.When(plr.when(condition))
@@ -0,0 +1,10 @@
1
+ """
2
+ Module containing the implementation of the Python dataframe interchange protocol.
3
+
4
+ Details on the protocol:
5
+ https://data-apis.org/dataframe-protocol/latest/index.html
6
+ """
7
+
8
+ from polars.interchange.protocol import CompatLevel
9
+
10
+ __all__ = ["CompatLevel"]