Path: blob/main/py-polars/tests/unit/dataframe/test_item.py
6939 views
from __future__ import annotations12import pytest34import polars as pl567def test_df_item() -> None:8df = pl.DataFrame({"a": [1]})9assert df.item() == 1101112def test_df_item_empty() -> None:13df = pl.DataFrame()14with pytest.raises(ValueError, match=r".* frame has shape \(0, 0\)"):15df.item()161718def test_df_item_incorrect_shape_rows() -> None:19df = pl.DataFrame({"a": [1, 2]})20with pytest.raises(ValueError, match=r".* frame has shape \(2, 1\)"):21df.item()222324def test_df_item_incorrect_shape_columns() -> None:25df = pl.DataFrame({"a": [1], "b": [2]})26with pytest.raises(ValueError, match=r".* frame has shape \(1, 2\)"):27df.item()282930@pytest.fixture(scope="module")31def df() -> pl.DataFrame:32return pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})333435@pytest.mark.parametrize(36("row", "col", "expected"),37[38(0, 0, 1),39(1, "a", 2),40(-1, 1, 6),41(-2, "b", 5),42],43)44def test_df_item_with_indices(45row: int, col: int | str, expected: int, df: pl.DataFrame46) -> None:47assert df.item(row, col) == expected484950def test_df_item_with_single_index(df: pl.DataFrame) -> None:51with pytest.raises(ValueError):52df.item(0)53with pytest.raises(ValueError):54df.item(column="b")55with pytest.raises(ValueError):56df.item(None, 0)575859@pytest.mark.parametrize(60("row", "col"), [(0, 10), (10, 0), (10, 10), (-10, 0), (-10, 10)]61)62def test_df_item_out_of_bounds(row: int, col: int, df: pl.DataFrame) -> None:63with pytest.raises(IndexError, match="out of bounds"):64df.item(row, col)656667