Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/interop/numpy/test_numpy.py
6939 views
1
from typing import Any
2
3
import numpy as np
4
import pytest
5
from numpy.testing import assert_array_equal
6
7
import polars as pl
8
from polars.testing import assert_series_equal
9
10
11
@pytest.fixture(
12
params=[
13
("int8", [1, 3, 2], pl.Int8, np.int8),
14
("int16", [1, 3, 2], pl.Int16, np.int16),
15
("int32", [1, 3, 2], pl.Int32, np.int32),
16
("int64", [1, 3, 2], pl.Int64, np.int64),
17
("uint8", [1, 3, 2], pl.UInt8, np.uint8),
18
("uint16", [1, 3, 2], pl.UInt16, np.uint16),
19
("uint32", [1, 3, 2], pl.UInt32, np.uint32),
20
("uint64", [1, 3, 2], pl.UInt64, np.uint64),
21
("float16", [-123.0, 0.0, 456.0], pl.Float32, np.float16),
22
("float32", [21.7, 21.8, 21], pl.Float32, np.float32),
23
("float64", [21.7, 21.8, 21], pl.Float64, np.float64),
24
("bool", [True, False, False], pl.Boolean, np.bool_),
25
("object", [21.7, "string1", object()], pl.Object, np.object_),
26
("str", ["string1", "string2", "string3"], pl.String, np.str_),
27
("intc", [1, 3, 2], pl.Int32, np.intc),
28
("uintc", [1, 3, 2], pl.UInt32, np.uintc),
29
("str_fixed", ["string1", "string2", "string3"], pl.String, np.str_),
30
(
31
"bytes",
32
[b"byte_string1", b"byte_string2", b"byte_string3"],
33
pl.Binary,
34
np.bytes_,
35
),
36
]
37
)
38
def numpy_interop_test_data(request: Any) -> Any:
39
return request.param
40
41
42
def test_df_from_numpy(numpy_interop_test_data: Any) -> None:
43
name, values, pl_dtype, np_dtype = numpy_interop_test_data
44
df = pl.DataFrame({name: np.array(values, dtype=np_dtype)})
45
assert [pl_dtype] == df.dtypes
46
47
48
def test_asarray(numpy_interop_test_data: Any) -> None:
49
name, values, pl_dtype, np_dtype = numpy_interop_test_data
50
pl_series_to_numpy_array = np.asarray(pl.Series(name, values, pl_dtype))
51
numpy_array = np.asarray(values, dtype=np_dtype)
52
assert_array_equal(pl_series_to_numpy_array, numpy_array)
53
54
55
def test_to_numpy(numpy_interop_test_data: Any) -> None:
56
name, values, pl_dtype, np_dtype = numpy_interop_test_data
57
pl_series_to_numpy_array = pl.Series(name, values, pl_dtype).to_numpy()
58
numpy_array = np.asarray(values, dtype=np_dtype)
59
assert_array_equal(pl_series_to_numpy_array, numpy_array)
60
61
62
def test_numpy_to_lit() -> None:
63
out = pl.select(pl.lit(np.array([1, 2, 3]))).to_series().to_list()
64
assert out == [1, 2, 3]
65
out = pl.select(pl.lit(np.float32(0))).to_series().to_list()
66
assert out == [0.0]
67
68
69
def test_numpy_disambiguation() -> None:
70
a = np.array([1, 2])
71
df = pl.DataFrame({"a": a})
72
result = df.with_columns(b=a).to_dict(as_series=False)
73
expected = {
74
"a": [1, 2],
75
"b": [1, 2],
76
}
77
assert result == expected
78
79
80
def test_respect_dtype_with_series_from_numpy() -> None:
81
assert pl.Series("foo", np.array([1, 2, 3]), dtype=pl.UInt32).dtype == pl.UInt32
82
83
84
@pytest.mark.parametrize(
85
("np_dtype_cls", "expected_pl_dtype"),
86
[
87
(np.int8, pl.Int8),
88
(np.int16, pl.Int16),
89
(np.int32, pl.Int32),
90
(np.int64, pl.Int64),
91
(np.uint8, pl.UInt8),
92
(np.uint16, pl.UInt16),
93
(np.uint32, pl.UInt32),
94
(np.uint64, pl.UInt64),
95
(np.float16, pl.Float32), # << note: we don't currently have a native f16
96
(np.float32, pl.Float32),
97
(np.float64, pl.Float64),
98
],
99
)
100
def test_init_from_numpy_values(np_dtype_cls: Any, expected_pl_dtype: Any) -> None:
101
# test init from raw numpy values (vs arrays)
102
s = pl.Series("n", [np_dtype_cls(0), np_dtype_cls(4), np_dtype_cls(8)])
103
assert s.dtype == expected_pl_dtype
104
105
106
def test_from_numpy_nonbit_bools_24296() -> None:
107
a = np.array([24, 15, 32, 1, 0], dtype=np.uint8).view(bool)
108
assert_series_equal(pl.Series(a), pl.Series([True, True, True, True, False]))
109
110