Skip to content
4 changes: 3 additions & 1 deletion python/cudf/benchmarks/API/bench_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ def bench_where(benchmark, dataframe, cond, other):
benchmark(dataframe.where, cond, other)


@benchmark_with_object(cls="dataframe", dtype="float", nulls=False, cols=20)
@benchmark_with_object(
cls="dataframe", dtype="float", nulls=False, cols=20, rows=20
)
@pytest.mark.pandas_incompatible
def bench_to_cupy(benchmark, dataframe):
benchmark(dataframe.to_cupy)
12 changes: 8 additions & 4 deletions python/cudf/benchmarks/API/bench_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ def bench_series_nsmallest(benchmark, series, n):
benchmark(series.nsmallest, n)


@benchmark_with_object(cls="series", dtype="int")
@benchmark_with_object(cls="series", dtype="int", nulls=False)
def bench_series_cp_asarray(benchmark, series):
series = series.dropna()
benchmark(cupy.asarray, series)


@benchmark_with_object(cls="series", dtype="int")
@benchmark_with_object(cls="series", dtype="int", nulls=False)
@pytest.mark.pandas_incompatible
def bench_to_cupy(benchmark, series):
benchmark(lambda: series.values)


@benchmark_with_object(cls="series", dtype="int", nulls=False)
def bench_series_values(benchmark, series):
series = series.dropna()
benchmark(lambda: series.values)
4 changes: 2 additions & 2 deletions python/cudf/benchmarks/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ def pytest_sessionfinish(session, exitstatus):
NUM_ROWS = [10, 20]
NUM_COLS = [1, 6, 20]
else:
NUM_ROWS = [100, 10_000, 1_000_000]
NUM_COLS = [1, 6, 20]
NUM_ROWS = [100, 1_000, 10_000, 50_000, 1_000_000]
NUM_COLS = [1, 6, 20, 1_000, 10_000, 50_000]
9 changes: 1 addition & 8 deletions python/cudf/benchmarks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"""

import os
import string
import sys

import pytest_cases
Expand Down Expand Up @@ -83,14 +82,8 @@ def axis(request):
for dtype, column_generator in column_generators.items():

def make_dataframe(nr, nc, column_generator=column_generator):
assert nc <= len(string.ascii_lowercase), (
"make_dataframe only supports a maximum of 26 columns"
)
return cudf.DataFrame(
{
f"{string.ascii_lowercase[i]}": column_generator(nr)
for i in range(nc)
}
{f"col{i}": column_generator(nr) for i in range(nc)}
)

for nr in NUM_ROWS:
Expand Down
13 changes: 6 additions & 7 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import TYPE_CHECKING, Any, Literal

import cupy
import cupy as cp
import numpy
import numpy as np
import pyarrow as pa
Expand Down Expand Up @@ -570,14 +569,14 @@ def to_cupy(
)
):
if dtype is None:
dtype = np.dtype(self._columns[0].dtype)
dtype = self._columns[0].dtype

shape = (len(self), self._num_columns)
out = cupy.empty(shape, dtype=dtype, order="F")

table = self.to_pylibcudf()[0]
if isinstance(table, plc.Column):
table = plc.Table([table])
table = plc.Table(
[col.to_pylibcudf(mode="read") for col in self._columns]
)
plc.reshape.table_to_array(
table,
out.data.ptr,
Expand All @@ -591,7 +590,7 @@ def to_cupy(
if (
not copy
and col.dtype.kind in {"i", "u", "f", "b"}
and cp.can_cast(col.dtype, final_dtype)
and cupy.can_cast(col.dtype, final_dtype)
):
if col.has_nulls():
if na_value is not None:
Expand All @@ -604,7 +603,7 @@ def to_cupy(
dtype,
na_value,
)
return cp.asarray(col, dtype=final_dtype).reshape((-1, 1))
return cupy.asarray(col, dtype=final_dtype).reshape((-1, 1))
return self._to_array(
lambda col: col.values,
cupy,
Expand Down
24 changes: 5 additions & 19 deletions python/cudf/cudf/core/single_column_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,11 @@ def to_cupy(
-------
cupy.ndarray
"""
col = self._column
final_dtype = (
col.dtype if dtype is None else dtype
) # some types do not support | operator
if (
not copy
and col.dtype.kind in {"i", "u", "f", "b"}
and cp.can_cast(col.dtype, final_dtype)
):
if col.has_nulls():
if na_value is not None:
col = col.fillna(na_value)
else:
return super().to_cupy(
dtype=dtype, copy=copy, na_value=na_value
)
return cp.asarray(col, dtype=final_dtype)

return super().to_cupy(dtype=dtype, copy=copy, na_value=na_value)
return (
super()
.to_cupy(dtype=dtype, copy=copy, na_value=na_value)
.reshape(len(self), order="F")
)

@property # type: ignore
@_performance_tracking
Expand Down
3 changes: 2 additions & 1 deletion python/pylibcudf/pylibcudf/reshape.pxd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2024-2025, NVIDIA CORPORATION.

from libc.stddef cimport size_t
from libc.stdint cimport uintptr_t

from pylibcudf.libcudf.types cimport size_type
Expand All @@ -18,6 +19,6 @@ cpdef Table tile(Table source_table, size_type count)
cpdef void table_to_array(
Table input_table,
uintptr_t ptr,
size_type size,
size_t size,
Stream stream=*
)
6 changes: 4 additions & 2 deletions python/pylibcudf/pylibcudf/reshape.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2024-2025, NVIDIA CORPORATION.

from libc.stddef cimport size_t
from libc.stdint cimport uintptr_t
from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move
Expand Down Expand Up @@ -81,11 +82,12 @@ cpdef Table tile(Table source_table, size_type count):
cpdef void table_to_array(
Table input_table,
uintptr_t ptr,
size_type size,
size_t size,
Stream stream=None
):
"""
Copy a table into a preallocated column-major device array.

Parameters
----------
input_table : Table
Expand All @@ -98,7 +100,7 @@ cpdef void table_to_array(
stream : Stream | None
CUDA stream on which to perform the operation.
"""
if size > numeric_limits[size_type].max():
if size > numeric_limits[size_t].max():
raise ValueError(
"Size exceeds the int32_t limit."
)
Expand Down
15 changes: 3 additions & 12 deletions python/pylibcudf/pylibcudf/tests/test_reshape.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2024-2025, NVIDIA CORPORATION.

import cupy as cp
import pyarrow as pa
import pytest
from utils import assert_column_eq, assert_table_eq
Expand All @@ -8,16 +9,6 @@
from pylibcudf.types import TypeId


@pytest.fixture(scope="module")
def np():
return pytest.importorskip("cupy")


@pytest.fixture(scope="module")
def cp():
return pytest.importorskip("cupy")


@pytest.fixture(scope="module")
def reshape_data():
data = [[1, 2, 3], [4, 5, 6]]
Expand Down Expand Up @@ -59,8 +50,8 @@ def test_tile(reshape_data, cnt):
("float64", TypeId.FLOAT64),
],
)
def test_table_to_array(dtype, type_id, np, cp):
arrow_type = pa.from_numpy_dtype(getattr(np, dtype))
def test_table_to_array(dtype, type_id):
arrow_type = pa.from_numpy_dtype(getattr(cp, dtype))
arrs = [
pa.array([1, 2, 3], type=arrow_type),
pa.array([4, 5, 6], type=arrow_type),
Expand Down
Loading