Skip to content

Commit

Permalink
Implement cast method on Array and ChunkedArray (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jul 30, 2024
1 parent 7a9eeaa commit 81bf8c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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

0 comments on commit 81bf8c8

Please sign in to comment.