Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/functions/test_col.py
8424 views
1
from __future__ import annotations
2
3
import polars as pl
4
from polars import col
5
from polars.datatypes.group import NUMERIC_DTYPES
6
from polars.testing import assert_frame_equal
7
8
9
def test_col_as_attribute() -> None:
10
df = pl.DataFrame({"lower": 1, "UPPER": 2, "_underscored": 3})
11
12
result = df.select(col.lower, col.UPPER, col._underscored)
13
expected = df.select("lower", "UPPER", "_underscored")
14
assert_frame_equal(result, expected)
15
16
17
def test_col_as_attribute_edge_cases() -> None:
18
df = pl.DataFrame(
19
{
20
"__misc": "x",
21
"__wrapped__col": "y",
22
"_other__col__": "z",
23
}
24
)
25
for select_cols in (
26
(pl.col("_other__col__"), pl.col("__wrapped__col"), pl.col("__misc")),
27
(pl.col._other__col__, pl.col.__wrapped__col, pl.col.__misc),
28
):
29
assert df.select(select_cols).columns == [
30
"_other__col__",
31
"__wrapped__col",
32
"__misc",
33
]
34
35
36
def test_col_as_attribute_class_mangling_25129() -> None:
37
# note: we have to run this test in a subprocess to prevent pytest
38
# itself from managing to inject "_pytestfixturefunction" as the
39
# col name/attribute, where we can't recover the original name
40
import subprocess
41
import sys
42
43
out = subprocess.check_output(
44
[
45
sys.executable,
46
"-c",
47
"""\
48
from sys import version_info
49
import polars as pl
50
df = pl.DataFrame({"__foo": [0]})
51
52
class Mangler:
53
def __init__(self):
54
self._selected = df.select(pl.col.__foo)
55
56
def foo(self):
57
return df.select(pl.col.__foo)
58
59
@classmethod
60
def misc(cls):
61
def _nested():
62
return df.select(pl.col.__foo)
63
return _nested()
64
65
@staticmethod
66
def indirect():
67
return Mangler.misc()
68
69
@staticmethod
70
def testing1234():
71
return df.select(pl.col.__foo)
72
73
74
# detect mangling in init/instancemethod
75
assert Mangler()._selected.columns == ["__foo"]
76
assert Mangler().foo().columns == ["__foo"]
77
78
# additionally detect mangling in classmethod/staticmethod
79
if version_info >= (3, 11):
80
assert Mangler.misc().columns == ["__foo"]
81
assert Mangler.indirect().columns == ["__foo"]
82
assert Mangler.testing1234().columns == ["__foo"]
83
84
print("OK", end="")
85
""",
86
],
87
)
88
assert out == b"OK"
89
90
91
def test_col_select() -> None:
92
df = pl.DataFrame(
93
{
94
"ham": [1, 2, 3],
95
"hamburger": [11, 22, 33],
96
"foo": [3, 2, 1],
97
"bar": ["a", "b", "c"],
98
}
99
)
100
101
# Single column
102
assert df.select(pl.col("foo")).columns == ["foo"]
103
104
# Regex
105
assert df.select(pl.col("*")).columns == ["ham", "hamburger", "foo", "bar"]
106
assert df.select(pl.col("^ham.*$")).columns == ["ham", "hamburger"]
107
assert df.select(pl.col("*").exclude("ham")).columns == ["hamburger", "foo", "bar"]
108
109
# Multiple inputs
110
assert df.select(pl.col(["hamburger", "foo"])).columns == ["hamburger", "foo"]
111
assert df.select(pl.col("hamburger", "foo")).columns == ["hamburger", "foo"]
112
assert df.select(pl.col(pl.Series(["ham", "foo"]))).columns == ["ham", "foo"]
113
114
# Dtypes
115
assert df.select(pl.col(pl.String)).columns == ["bar"]
116
for dtype_col in (
117
pl.col(NUMERIC_DTYPES),
118
pl.col(pl.Int64, pl.Float64),
119
):
120
assert df.select(dtype_col).columns == ["ham", "hamburger", "foo"]
121
122
123
def test_col_series_selection() -> None:
124
ldf = pl.LazyFrame({"a": [1], "b": [1], "c": [1]})
125
srs = pl.Series(["b", "c"])
126
127
assert ldf.select(pl.col(srs)).collect_schema().names() == ["b", "c"]
128
129