Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/dataframe/test_item.py
6939 views
1
from __future__ import annotations
2
3
import pytest
4
5
import polars as pl
6
7
8
def test_df_item() -> None:
9
df = pl.DataFrame({"a": [1]})
10
assert df.item() == 1
11
12
13
def test_df_item_empty() -> None:
14
df = pl.DataFrame()
15
with pytest.raises(ValueError, match=r".* frame has shape \(0, 0\)"):
16
df.item()
17
18
19
def test_df_item_incorrect_shape_rows() -> None:
20
df = pl.DataFrame({"a": [1, 2]})
21
with pytest.raises(ValueError, match=r".* frame has shape \(2, 1\)"):
22
df.item()
23
24
25
def test_df_item_incorrect_shape_columns() -> None:
26
df = pl.DataFrame({"a": [1], "b": [2]})
27
with pytest.raises(ValueError, match=r".* frame has shape \(1, 2\)"):
28
df.item()
29
30
31
@pytest.fixture(scope="module")
32
def df() -> pl.DataFrame:
33
return pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
34
35
36
@pytest.mark.parametrize(
37
("row", "col", "expected"),
38
[
39
(0, 0, 1),
40
(1, "a", 2),
41
(-1, 1, 6),
42
(-2, "b", 5),
43
],
44
)
45
def test_df_item_with_indices(
46
row: int, col: int | str, expected: int, df: pl.DataFrame
47
) -> None:
48
assert df.item(row, col) == expected
49
50
51
def test_df_item_with_single_index(df: pl.DataFrame) -> None:
52
with pytest.raises(ValueError):
53
df.item(0)
54
with pytest.raises(ValueError):
55
df.item(column="b")
56
with pytest.raises(ValueError):
57
df.item(None, 0)
58
59
60
@pytest.mark.parametrize(
61
("row", "col"), [(0, 10), (10, 0), (10, 10), (-10, 0), (-10, 10)]
62
)
63
def test_df_item_out_of_bounds(row: int, col: int, df: pl.DataFrame) -> None:
64
with pytest.raises(IndexError, match="out of bounds"):
65
df.item(row, col)
66
67