Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cast method on Array and ChunkedArray #79

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions arro3-core/python/arro3/core/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class Array:
def to_numpy(self) -> NDArray:
"""Return a numpy copy of this array."""

def cast(self, target_type: ArrowSchemaExportable) -> Array:
"""Cast array values to another data type

Args:
target_type: Type to cast array to.
"""

def slice(self, offset: int = 0, length: int | None = None) -> Array:
"""Compute zero-copy slice of this array.

Expand Down Expand Up @@ -100,6 +107,12 @@ class ChunkedArray:
@classmethod
def from_arrow_pycapsule(cls, capsule) -> ChunkedArray:
"""Construct this object from a bare Arrow PyCapsule"""
def cast(self, target_type: ArrowSchemaExportable) -> ChunkedArray:
"""Cast array values to another data type

Args:
target_type: Type to cast array to.
"""
def chunk(self, i: int) -> Array: ...
@property
def chunks(self) -> list[Array]: ...
Expand Down
7 changes: 7 additions & 0 deletions pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ impl PyArray {
Ok(Self::from_array_ref(arrow_array))
}

fn cast(&self, py: Python, target_type: PyDataType) -> PyArrowResult<PyObject> {
let target_type = target_type.into_inner();
let new_array = arrow::compute::cast(self.as_ref(), &target_type)?;
let new_field = self.field.as_ref().clone().with_data_type(target_type);
Ok(PyArray::new(new_array, new_field.into()).to_arro3(py)?)
}

#[pyo3(signature = (offset=0, length=None))]
pub fn slice(&self, py: Python, offset: usize, length: Option<usize>) -> PyResult<PyObject> {
let length = length.unwrap_or_else(|| self.array.len() - offset);
Expand Down
11 changes: 11 additions & 0 deletions pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ impl PyChunkedArray {
Ok(PyChunkedArray::new(chunks, field))
}

fn cast(&self, py: Python, target_type: PyDataType) -> PyArrowResult<PyObject> {
let target_type = target_type.into_inner();
let new_chunks = self
.chunks
.iter()
.map(|chunk| arrow::compute::cast(&chunk, &target_type))
.collect::<Result<Vec<_>, ArrowError>>()?;
let new_field = self.field.as_ref().clone().with_data_type(target_type);
Ok(PyChunkedArray::new(new_chunks, new_field.into()).to_arro3(py)?)
}

pub fn chunk(&self, py: Python, i: usize) -> PyResult<PyObject> {
let field = self.field().clone();
let array = self
Expand Down