Path: blob/main/py-polars/tests/unit/interop/numpy/test_numpy.py
6939 views
from typing import Any12import numpy as np3import pytest4from numpy.testing import assert_array_equal56import polars as pl7from polars.testing import assert_series_equal8910@pytest.fixture(11params=[12("int8", [1, 3, 2], pl.Int8, np.int8),13("int16", [1, 3, 2], pl.Int16, np.int16),14("int32", [1, 3, 2], pl.Int32, np.int32),15("int64", [1, 3, 2], pl.Int64, np.int64),16("uint8", [1, 3, 2], pl.UInt8, np.uint8),17("uint16", [1, 3, 2], pl.UInt16, np.uint16),18("uint32", [1, 3, 2], pl.UInt32, np.uint32),19("uint64", [1, 3, 2], pl.UInt64, np.uint64),20("float16", [-123.0, 0.0, 456.0], pl.Float32, np.float16),21("float32", [21.7, 21.8, 21], pl.Float32, np.float32),22("float64", [21.7, 21.8, 21], pl.Float64, np.float64),23("bool", [True, False, False], pl.Boolean, np.bool_),24("object", [21.7, "string1", object()], pl.Object, np.object_),25("str", ["string1", "string2", "string3"], pl.String, np.str_),26("intc", [1, 3, 2], pl.Int32, np.intc),27("uintc", [1, 3, 2], pl.UInt32, np.uintc),28("str_fixed", ["string1", "string2", "string3"], pl.String, np.str_),29(30"bytes",31[b"byte_string1", b"byte_string2", b"byte_string3"],32pl.Binary,33np.bytes_,34),35]36)37def numpy_interop_test_data(request: Any) -> Any:38return request.param394041def test_df_from_numpy(numpy_interop_test_data: Any) -> None:42name, values, pl_dtype, np_dtype = numpy_interop_test_data43df = pl.DataFrame({name: np.array(values, dtype=np_dtype)})44assert [pl_dtype] == df.dtypes454647def test_asarray(numpy_interop_test_data: Any) -> None:48name, values, pl_dtype, np_dtype = numpy_interop_test_data49pl_series_to_numpy_array = np.asarray(pl.Series(name, values, pl_dtype))50numpy_array = np.asarray(values, dtype=np_dtype)51assert_array_equal(pl_series_to_numpy_array, numpy_array)525354def test_to_numpy(numpy_interop_test_data: Any) -> None:55name, values, pl_dtype, np_dtype = numpy_interop_test_data56pl_series_to_numpy_array = pl.Series(name, values, pl_dtype).to_numpy()57numpy_array = np.asarray(values, dtype=np_dtype)58assert_array_equal(pl_series_to_numpy_array, numpy_array)596061def test_numpy_to_lit() -> None:62out = pl.select(pl.lit(np.array([1, 2, 3]))).to_series().to_list()63assert out == [1, 2, 3]64out = pl.select(pl.lit(np.float32(0))).to_series().to_list()65assert out == [0.0]666768def test_numpy_disambiguation() -> None:69a = np.array([1, 2])70df = pl.DataFrame({"a": a})71result = df.with_columns(b=a).to_dict(as_series=False)72expected = {73"a": [1, 2],74"b": [1, 2],75}76assert result == expected777879def test_respect_dtype_with_series_from_numpy() -> None:80assert pl.Series("foo", np.array([1, 2, 3]), dtype=pl.UInt32).dtype == pl.UInt32818283@pytest.mark.parametrize(84("np_dtype_cls", "expected_pl_dtype"),85[86(np.int8, pl.Int8),87(np.int16, pl.Int16),88(np.int32, pl.Int32),89(np.int64, pl.Int64),90(np.uint8, pl.UInt8),91(np.uint16, pl.UInt16),92(np.uint32, pl.UInt32),93(np.uint64, pl.UInt64),94(np.float16, pl.Float32), # << note: we don't currently have a native f1695(np.float32, pl.Float32),96(np.float64, pl.Float64),97],98)99def test_init_from_numpy_values(np_dtype_cls: Any, expected_pl_dtype: Any) -> None:100# test init from raw numpy values (vs arrays)101s = pl.Series("n", [np_dtype_cls(0), np_dtype_cls(4), np_dtype_cls(8)])102assert s.dtype == expected_pl_dtype103104105def test_from_numpy_nonbit_bools_24296() -> None:106a = np.array([24, 15, 32, 1, 0], dtype=np.uint8).view(bool)107assert_series_equal(pl.Series(a), pl.Series([True, True, True, True, False]))108109110