Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/centimators/narwhals_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _ensure_numpy(data, allow_series: bool = False):
"""Convert data to numpy array, handling both numpy arrays and dataframes.

Args:
data: Input data (numpy array, dataframe, or series)
data: Input data (numpy array, dataframe, series, or PyTorch tensor)
allow_series: Whether to allow series inputs

Returns:
Expand All @@ -24,6 +24,14 @@ def _ensure_numpy(data, allow_series: bool = False):
try:
return nw.from_native(data, allow_series=allow_series).to_numpy()
except Exception:
# Handle PyTorch tensors (including CUDA tensors)
try:
import torch
if isinstance(data, torch.Tensor):
# Move to CPU if on GPU, then convert to numpy
return data.detach().cpu().numpy()
except ImportError:
pass
return numpy.asarray(data)


Expand Down