-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* From numpy * fix lint * remove unwrap * cleanup
- Loading branch information
1 parent
6bcd421
commit 264f20f
Showing
12 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow_array::cast::AsArray; | ||
use arrow_array::types::{Int32Type, Int64Type}; | ||
use arrow_array::{Array, ArrayRef, FixedSizeListArray, LargeListArray, ListArray, StructArray}; | ||
use arrow_buffer::OffsetBuffer; | ||
use arrow_schema::{DataType, Field}; | ||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
|
||
use pyo3_arrow::error::PyArrowResult; | ||
use pyo3_arrow::{PyArray, PyField}; | ||
|
||
#[pyfunction] | ||
#[pyo3(signature=(values, list_size, *, r#type=None))] | ||
#[allow(dead_code)] | ||
fn fixed_size_list_array( | ||
py: Python, | ||
values: PyArray, | ||
list_size: i32, | ||
r#type: Option<PyField>, | ||
) -> PyArrowResult<PyObject> { | ||
let (values_array, values_field) = values.into_inner(); | ||
let field = r#type.map(|f| f.into_inner()).unwrap_or_else(|| { | ||
Arc::new(Field::new_fixed_size_list( | ||
"", | ||
values_field, | ||
list_size, | ||
true, | ||
)) | ||
}); | ||
|
||
let array = FixedSizeListArray::try_new(field.clone(), list_size, values_array, None)?; | ||
Ok(PyArray::new(Arc::new(array), field).to_arro3(py)?) | ||
} | ||
|
||
#[pyfunction] | ||
#[pyo3(signature=(offsets, values, *, r#type=None))] | ||
#[allow(dead_code)] | ||
fn list_array( | ||
py: Python, | ||
offsets: PyArray, | ||
values: PyArray, | ||
r#type: Option<PyField>, | ||
) -> PyArrowResult<PyObject> { | ||
let (values_array, values_field) = values.into_inner(); | ||
let (offsets_array, _) = offsets.into_inner(); | ||
let large_offsets = match offsets_array.data_type() { | ||
DataType::Int32 => false, | ||
DataType::Int64 => true, | ||
_ => { | ||
return Err( | ||
PyValueError::new_err("Expected offsets to have int32 or int64 type").into(), | ||
) | ||
} | ||
}; | ||
let field = r#type.map(|f| f.into_inner()).unwrap_or_else(|| { | ||
if large_offsets { | ||
Arc::new(Field::new_large_list("item", values_field, true)) | ||
} else { | ||
Arc::new(Field::new_list("item", values_field, true)) | ||
} | ||
}); | ||
|
||
let list_array: ArrayRef = if large_offsets { | ||
Arc::new(LargeListArray::try_new( | ||
field.clone(), | ||
OffsetBuffer::new(offsets_array.as_primitive::<Int64Type>().values().clone()), | ||
values_array, | ||
None, | ||
)?) | ||
} else { | ||
Arc::new(ListArray::try_new( | ||
field.clone(), | ||
OffsetBuffer::new(offsets_array.as_primitive::<Int32Type>().values().clone()), | ||
values_array, | ||
None, | ||
)?) | ||
}; | ||
Ok(PyArray::new(Arc::new(list_array), field).to_arro3(py)?) | ||
} | ||
|
||
#[pyfunction] | ||
#[pyo3(signature=(arrays, *, fields))] | ||
#[allow(dead_code)] | ||
fn struct_array(py: Python, arrays: Vec<PyArray>, fields: Vec<PyField>) -> PyArrowResult<PyObject> { | ||
let arrays = arrays | ||
.into_iter() | ||
.map(|arr| { | ||
let (arr, _field) = arr.into_inner(); | ||
arr | ||
}) | ||
.collect::<Vec<_>>(); | ||
let fields = fields | ||
.into_iter() | ||
.map(|field| field.into_inner()) | ||
.collect::<Vec<_>>(); | ||
|
||
let array = StructArray::try_new(fields.clone().into(), arrays, None)?; | ||
let field = Field::new_struct("", fields, true); | ||
Ok(PyArray::new(Arc::new(array), field.into()).to_arro3(py)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use pyo3::prelude::*; | ||
|
||
mod constructors; | ||
|
||
const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
#[pyfunction] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::ffi::from_python::utils::call_arrow_c_schema; | ||
use crate::field::PyField; | ||
use crate::PyDataType; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyDataType { | ||
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> { | ||
let capsule = call_arrow_c_schema(ob)?; | ||
Python::with_gil(|py| Self::from_arrow_pycapsule(&py.get_type_bound::<PyField>(), &capsule)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod array; | ||
mod array_reader; | ||
mod chunked; | ||
mod datatypes; | ||
pub(crate) mod ffi_stream; | ||
mod field; | ||
mod input; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow::datatypes::{ | ||
Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type, | ||
UInt64Type, UInt8Type, | ||
}; | ||
use arrow_array::{ArrayRef, BooleanArray, PrimitiveArray}; | ||
use arrow_buffer::BooleanBuffer; | ||
use arrow_schema::DataType; | ||
use numpy::{PyArray1, PyUntypedArray}; | ||
use pyo3::exceptions::PyValueError; | ||
|
||
use crate::error::PyArrowResult; | ||
|
||
pub fn from_numpy(array: &PyUntypedArray, arrow_data_type: DataType) -> PyArrowResult<ArrayRef> { | ||
macro_rules! numpy_to_arrow { | ||
($dtype:ty, $arrow_type:ty) => {{ | ||
let arr = array.downcast::<PyArray1<$dtype>>()?; | ||
Ok(Arc::new(PrimitiveArray::<$arrow_type>::from( | ||
arr.to_owned_array().to_vec(), | ||
))) | ||
}}; | ||
} | ||
|
||
match arrow_data_type { | ||
// DataType::Float16 => numpy_to_arrow!(f16, Float16Type), | ||
DataType::Float32 => numpy_to_arrow!(f32, Float32Type), | ||
DataType::Float64 => numpy_to_arrow!(f64, Float64Type), | ||
DataType::UInt8 => numpy_to_arrow!(u8, UInt8Type), | ||
DataType::UInt16 => numpy_to_arrow!(u16, UInt16Type), | ||
DataType::UInt32 => numpy_to_arrow!(u32, UInt32Type), | ||
DataType::UInt64 => numpy_to_arrow!(u64, UInt64Type), | ||
DataType::Int8 => numpy_to_arrow!(i8, Int8Type), | ||
DataType::Int16 => numpy_to_arrow!(i16, Int16Type), | ||
DataType::Int32 => numpy_to_arrow!(i32, Int32Type), | ||
DataType::Int64 => numpy_to_arrow!(i64, Int64Type), | ||
DataType::Boolean => { | ||
let arr = array.downcast::<PyArray1<bool>>()?; | ||
let buffer = BooleanBuffer::from(arr.to_owned_array().to_vec()); | ||
Ok(Arc::new(BooleanArray::new(buffer, None))) | ||
} | ||
_ => { | ||
Err(PyValueError::new_err(format!("Unsupported data type {}", arrow_data_type)).into()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod to_numpy; | ||
pub(crate) mod from_numpy; | ||
pub(crate) mod to_numpy; |