polars-runtime-compat 1.34.0b2__cp39-abi3-win_arm64.whl → 1.34.0b4__cp39-abi3-win_arm64.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.
- _polars_runtime_compat/_polars_runtime_compat.pyd +0 -0
- {polars_runtime_compat-1.34.0b2.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/METADATA +1 -1
- polars_runtime_compat-1.34.0b4.dist-info/RECORD +6 -0
- polars/__init__.py +0 -528
- polars/_cpu_check.py +0 -265
- polars/_dependencies.py +0 -355
- polars/_plr.py +0 -99
- polars/_plr.pyi +0 -2496
- polars/_reexport.py +0 -23
- polars/_typing.py +0 -478
- polars/_utils/__init__.py +0 -37
- polars/_utils/async_.py +0 -102
- polars/_utils/cache.py +0 -176
- polars/_utils/cloud.py +0 -40
- polars/_utils/constants.py +0 -29
- polars/_utils/construction/__init__.py +0 -46
- polars/_utils/construction/dataframe.py +0 -1397
- polars/_utils/construction/other.py +0 -72
- polars/_utils/construction/series.py +0 -560
- polars/_utils/construction/utils.py +0 -118
- polars/_utils/convert.py +0 -224
- polars/_utils/deprecation.py +0 -406
- polars/_utils/getitem.py +0 -457
- polars/_utils/logging.py +0 -11
- polars/_utils/nest_asyncio.py +0 -264
- polars/_utils/parquet.py +0 -15
- polars/_utils/parse/__init__.py +0 -12
- polars/_utils/parse/expr.py +0 -242
- polars/_utils/polars_version.py +0 -19
- polars/_utils/pycapsule.py +0 -53
- polars/_utils/scan.py +0 -27
- polars/_utils/serde.py +0 -63
- polars/_utils/slice.py +0 -215
- polars/_utils/udfs.py +0 -1251
- polars/_utils/unstable.py +0 -63
- polars/_utils/various.py +0 -782
- polars/_utils/wrap.py +0 -25
- polars/api.py +0 -370
- polars/catalog/__init__.py +0 -0
- polars/catalog/unity/__init__.py +0 -19
- polars/catalog/unity/client.py +0 -733
- polars/catalog/unity/models.py +0 -152
- polars/config.py +0 -1571
- polars/convert/__init__.py +0 -25
- polars/convert/general.py +0 -1046
- polars/convert/normalize.py +0 -261
- polars/dataframe/__init__.py +0 -5
- polars/dataframe/_html.py +0 -186
- polars/dataframe/frame.py +0 -12582
- polars/dataframe/group_by.py +0 -1067
- polars/dataframe/plotting.py +0 -257
- polars/datatype_expr/__init__.py +0 -5
- polars/datatype_expr/array.py +0 -56
- polars/datatype_expr/datatype_expr.py +0 -304
- polars/datatype_expr/list.py +0 -18
- polars/datatype_expr/struct.py +0 -69
- polars/datatypes/__init__.py +0 -122
- polars/datatypes/_parse.py +0 -195
- polars/datatypes/_utils.py +0 -48
- polars/datatypes/classes.py +0 -1213
- polars/datatypes/constants.py +0 -11
- polars/datatypes/constructor.py +0 -172
- polars/datatypes/convert.py +0 -366
- polars/datatypes/group.py +0 -130
- polars/exceptions.py +0 -230
- polars/expr/__init__.py +0 -7
- polars/expr/array.py +0 -964
- polars/expr/binary.py +0 -346
- polars/expr/categorical.py +0 -306
- polars/expr/datetime.py +0 -2620
- polars/expr/expr.py +0 -11272
- polars/expr/list.py +0 -1408
- polars/expr/meta.py +0 -444
- polars/expr/name.py +0 -321
- polars/expr/string.py +0 -3045
- polars/expr/struct.py +0 -357
- polars/expr/whenthen.py +0 -185
- polars/functions/__init__.py +0 -193
- polars/functions/aggregation/__init__.py +0 -33
- polars/functions/aggregation/horizontal.py +0 -298
- polars/functions/aggregation/vertical.py +0 -341
- polars/functions/as_datatype.py +0 -848
- polars/functions/business.py +0 -138
- polars/functions/col.py +0 -384
- polars/functions/datatype.py +0 -121
- polars/functions/eager.py +0 -524
- polars/functions/escape_regex.py +0 -29
- polars/functions/lazy.py +0 -2751
- polars/functions/len.py +0 -68
- polars/functions/lit.py +0 -210
- polars/functions/random.py +0 -22
- polars/functions/range/__init__.py +0 -19
- polars/functions/range/_utils.py +0 -15
- polars/functions/range/date_range.py +0 -303
- polars/functions/range/datetime_range.py +0 -370
- polars/functions/range/int_range.py +0 -348
- polars/functions/range/linear_space.py +0 -311
- polars/functions/range/time_range.py +0 -287
- polars/functions/repeat.py +0 -301
- polars/functions/whenthen.py +0 -353
- polars/interchange/__init__.py +0 -10
- polars/interchange/buffer.py +0 -77
- polars/interchange/column.py +0 -190
- polars/interchange/dataframe.py +0 -230
- polars/interchange/from_dataframe.py +0 -328
- polars/interchange/protocol.py +0 -303
- polars/interchange/utils.py +0 -170
- polars/io/__init__.py +0 -64
- polars/io/_utils.py +0 -317
- polars/io/avro.py +0 -49
- polars/io/clipboard.py +0 -36
- polars/io/cloud/__init__.py +0 -17
- polars/io/cloud/_utils.py +0 -80
- polars/io/cloud/credential_provider/__init__.py +0 -17
- polars/io/cloud/credential_provider/_builder.py +0 -520
- polars/io/cloud/credential_provider/_providers.py +0 -618
- polars/io/csv/__init__.py +0 -9
- polars/io/csv/_utils.py +0 -38
- polars/io/csv/batched_reader.py +0 -142
- polars/io/csv/functions.py +0 -1495
- polars/io/database/__init__.py +0 -6
- polars/io/database/_arrow_registry.py +0 -70
- polars/io/database/_cursor_proxies.py +0 -147
- polars/io/database/_executor.py +0 -578
- polars/io/database/_inference.py +0 -314
- polars/io/database/_utils.py +0 -144
- polars/io/database/functions.py +0 -516
- polars/io/delta.py +0 -499
- polars/io/iceberg/__init__.py +0 -3
- polars/io/iceberg/_utils.py +0 -697
- polars/io/iceberg/dataset.py +0 -556
- polars/io/iceberg/functions.py +0 -151
- polars/io/ipc/__init__.py +0 -8
- polars/io/ipc/functions.py +0 -514
- polars/io/json/__init__.py +0 -3
- polars/io/json/read.py +0 -101
- polars/io/ndjson.py +0 -332
- polars/io/parquet/__init__.py +0 -17
- polars/io/parquet/field_overwrites.py +0 -140
- polars/io/parquet/functions.py +0 -722
- polars/io/partition.py +0 -491
- polars/io/plugins.py +0 -187
- polars/io/pyarrow_dataset/__init__.py +0 -5
- polars/io/pyarrow_dataset/anonymous_scan.py +0 -109
- polars/io/pyarrow_dataset/functions.py +0 -79
- polars/io/scan_options/__init__.py +0 -5
- polars/io/scan_options/_options.py +0 -59
- polars/io/scan_options/cast_options.py +0 -126
- polars/io/spreadsheet/__init__.py +0 -6
- polars/io/spreadsheet/_utils.py +0 -52
- polars/io/spreadsheet/_write_utils.py +0 -647
- polars/io/spreadsheet/functions.py +0 -1323
- polars/lazyframe/__init__.py +0 -9
- polars/lazyframe/engine_config.py +0 -61
- polars/lazyframe/frame.py +0 -8564
- polars/lazyframe/group_by.py +0 -669
- polars/lazyframe/in_process.py +0 -42
- polars/lazyframe/opt_flags.py +0 -333
- polars/meta/__init__.py +0 -14
- polars/meta/build.py +0 -33
- polars/meta/index_type.py +0 -27
- polars/meta/thread_pool.py +0 -50
- polars/meta/versions.py +0 -120
- polars/ml/__init__.py +0 -0
- polars/ml/torch.py +0 -213
- polars/ml/utilities.py +0 -30
- polars/plugins.py +0 -155
- polars/py.typed +0 -0
- polars/pyproject.toml +0 -96
- polars/schema.py +0 -265
- polars/selectors.py +0 -3117
- polars/series/__init__.py +0 -5
- polars/series/array.py +0 -776
- polars/series/binary.py +0 -254
- polars/series/categorical.py +0 -246
- polars/series/datetime.py +0 -2275
- polars/series/list.py +0 -1087
- polars/series/plotting.py +0 -191
- polars/series/series.py +0 -9197
- polars/series/string.py +0 -2367
- polars/series/struct.py +0 -154
- polars/series/utils.py +0 -191
- polars/sql/__init__.py +0 -7
- polars/sql/context.py +0 -677
- polars/sql/functions.py +0 -139
- polars/string_cache.py +0 -185
- polars/testing/__init__.py +0 -13
- polars/testing/asserts/__init__.py +0 -9
- polars/testing/asserts/frame.py +0 -231
- polars/testing/asserts/series.py +0 -219
- polars/testing/asserts/utils.py +0 -12
- polars/testing/parametric/__init__.py +0 -33
- polars/testing/parametric/profiles.py +0 -107
- polars/testing/parametric/strategies/__init__.py +0 -22
- polars/testing/parametric/strategies/_utils.py +0 -14
- polars/testing/parametric/strategies/core.py +0 -615
- polars/testing/parametric/strategies/data.py +0 -452
- polars/testing/parametric/strategies/dtype.py +0 -436
- polars/testing/parametric/strategies/legacy.py +0 -169
- polars/type_aliases.py +0 -24
- polars_runtime_compat-1.34.0b2.dist-info/RECORD +0 -203
- {polars_runtime_compat-1.34.0b2.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/WHEEL +0 -0
- {polars_runtime_compat-1.34.0b2.dist-info → polars_runtime_compat-1.34.0b4.dist-info}/licenses/LICENSE +0 -0
polars/functions/repeat.py
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
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")
|
polars/functions/whenthen.py
DELETED
|
@@ -1,353 +0,0 @@
|
|
|
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))
|
polars/interchange/__init__.py
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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"]
|