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_nth.py
6939 views
1
from __future__ import annotations
2
3
import pytest
4
5
import polars as pl
6
from polars.exceptions import DuplicateError
7
from polars.testing import assert_frame_equal
8
9
10
@pytest.mark.parametrize(
11
("expr", "expected_cols"),
12
[
13
(pl.nth(0), "a"),
14
(pl.nth(-1), "c"),
15
(pl.nth(2, 1), ["c", "b"]),
16
(pl.nth([2, -2, 0]), ["c", "b", "a"]),
17
],
18
)
19
def test_nth(expr: pl.Expr, expected_cols: list[str]) -> None:
20
df = pl.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
21
result = df.select(expr)
22
expected = df.select(expected_cols)
23
assert_frame_equal(result, expected)
24
25
26
def test_nth_duplicate() -> None:
27
df = pl.DataFrame({"a": [1, 2]})
28
with pytest.raises(DuplicateError, match="a"):
29
df.select(pl.nth(0, 0))
30
31