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_array_method.py
6939 views
1
import numpy as np
2
import pytest
3
from numpy.testing import assert_array_equal
4
5
import polars as pl
6
7
8
def test_series_array_method_copy_false() -> None:
9
s = pl.Series([1, 2, None])
10
with pytest.raises(RuntimeError, match="copy not allowed"):
11
s.__array__(copy=False)
12
13
result = s.__array__(copy=None)
14
expected = np.array([1.0, 2.0, np.nan])
15
assert_array_equal(result, expected)
16
17
18
@pytest.mark.parametrize("copy", [True, False])
19
def test_series_array_method_copy_zero_copy(copy: bool) -> None:
20
s = pl.Series([1, 2, 3])
21
result = s.__array__(copy=copy)
22
23
assert result.flags.writeable is copy
24
25
26
def test_df_array_method() -> None:
27
df = pl.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
28
29
out_array = np.asarray(df, order="F")
30
expected_array = np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]], dtype=np.float64)
31
assert_array_equal(out_array, expected_array)
32
assert out_array.flags["F_CONTIGUOUS"] is True
33
34
out_array = np.asarray(df, dtype=np.uint8, order="C")
35
expected_array = np.array([[1, 1], [2, 2], [3, 3]], dtype=np.uint8)
36
assert_array_equal(out_array, expected_array)
37
assert out_array.flags["C_CONTIGUOUS"] is True
38
39