Path: blob/main/py-polars/tests/unit/test_expr_multi_cols.py
6939 views
import polars as pl1from polars.testing import assert_frame_equal234def test_exclude_name_from_dtypes() -> None:5df = pl.DataFrame({"a": ["a"], "b": ["b"]})67assert_frame_equal(8df.with_columns(pl.col(pl.String).exclude("a").name.suffix("_foo")),9pl.DataFrame({"a": ["a"], "b": ["b"], "b_foo": ["b"]}),10)111213def test_fold_regex_expand() -> None:14df = pl.DataFrame(15{16"x": [0, 1, 2],17"y_1": [1.1, 2.2, 3.3],18"y_2": [1.0, 2.5, 3.5],19}20)21assert df.with_columns(22pl.fold(23acc=pl.lit(0.0), function=lambda acc, x: acc + x, exprs=pl.col("^y_.*$")24).alias("y_sum"),25).to_dict(as_series=False) == {26"x": [0, 1, 2],27"y_1": [1.1, 2.2, 3.3],28"y_2": [1.0, 2.5, 3.5],29"y_sum": [2.1, 4.7, 6.8],30}313233def test_arg_sort_argument_expansion() -> None:34df = pl.DataFrame(35{36"col1": [1, 2, 3],37"col2": [4, 5, 6],38"sort_order": [9, 8, 7],39}40)41assert df.select(42pl.col("col1").sort_by(pl.col("sort_order").arg_sort()).name.suffix("_suffix")43).to_dict(as_series=False) == {"col1_suffix": [3, 2, 1]}44assert df.select(45pl.col("^col.*$").sort_by(pl.col("sort_order")).arg_sort()46).to_dict(as_series=False) == {"col1": [2, 1, 0], "col2": [2, 1, 0]}47assert df.select(48pl.all().exclude("sort_order").sort_by(pl.col("sort_order")).arg_sort()49).to_dict(as_series=False) == {"col1": [2, 1, 0], "col2": [2, 1, 0]}505152def test_multiple_columns_length_9137() -> None:53df = pl.DataFrame(54{55"a": [1, 1],56"b": ["c", "d"],57}58)5960# list is larger than groups61cmp_list = ["a", "b", "c"]6263assert df.group_by("a").agg(pl.col("b").is_in(cmp_list)).to_dict(64as_series=False65) == {66"a": [1],67"b": [[True, False]],68}697071def test_regex_in_cols() -> None:72df = pl.DataFrame(73{74"col1": [1, 2, 3],75"col2": [4, 5, 6],76"val1": ["a", "b", "c"],77"val2": ["A", "B", "C"],78}79)8081assert df.select(pl.col("^col.*$").name.prefix("matched_")).to_dict(82as_series=False83) == {84"matched_col1": [1, 2, 3],85"matched_col2": [4, 5, 6],86}8788assert df.with_columns(89pl.col("^col.*$", "^val.*$").name.prefix("matched_")90).to_dict(as_series=False) == {91"col1": [1, 2, 3],92"col2": [4, 5, 6],93"val1": ["a", "b", "c"],94"val2": ["A", "B", "C"],95"matched_col1": [1, 2, 3],96"matched_col2": [4, 5, 6],97"matched_val1": ["a", "b", "c"],98"matched_val2": ["A", "B", "C"],99}100assert df.select(pl.col("^col.*$", "val1").name.prefix("matched_")).to_dict(101as_series=False102) == {103"matched_col1": [1, 2, 3],104"matched_col2": [4, 5, 6],105"matched_val1": ["a", "b", "c"],106}107108assert df.select(pl.col("^col.*$", "val1").exclude("col2")).to_dict(109as_series=False110) == {111"col1": [1, 2, 3],112"val1": ["a", "b", "c"],113}114115116