Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/benchmark/interop/test_numpy.py
6940 views
1
"""Benchmark tests for conversions from/to NumPy."""
2
3
from __future__ import annotations
4
5
from typing import Any
6
7
import numpy as np
8
import pytest
9
10
import polars as pl
11
12
pytestmark = pytest.mark.benchmark()
13
14
15
@pytest.fixture(scope="module")
16
def floats_array() -> np.ndarray[Any, Any]:
17
n_rows = 10_000
18
return np.random.randn(n_rows)
19
20
21
@pytest.fixture
22
def floats(floats_array: np.ndarray[Any, Any]) -> pl.Series:
23
return pl.Series(floats_array)
24
25
26
@pytest.fixture
27
def floats_with_nulls(floats: pl.Series) -> pl.Series:
28
null_probability = 0.1
29
validity = pl.Series(np.random.uniform(size=floats.len())) > null_probability
30
return pl.select(pl.when(validity).then(floats)).to_series()
31
32
33
@pytest.fixture
34
def floats_chunked(floats_array: np.ndarray[Any, Any]) -> pl.Series:
35
n_chunks = 5
36
chunk_len = len(floats_array) // n_chunks
37
chunks = [
38
floats_array[i * chunk_len : (i + 1) * chunk_len] for i in range(n_chunks)
39
]
40
chunks_copy = [pl.Series(c.copy()) for c in chunks]
41
return pl.concat(chunks_copy, rechunk=False)
42
43
44
def test_to_numpy_series_zero_copy(floats: pl.Series) -> None:
45
floats.to_numpy()
46
47
48
def test_to_numpy_series_with_nulls(floats_with_nulls: pl.Series) -> None:
49
floats_with_nulls.to_numpy()
50
51
52
def test_to_numpy_series_chunked(floats_chunked: pl.Series) -> None:
53
floats_chunked.to_numpy()
54
55