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

To pyarrow with schema #3188

Merged
merged 4 commits into from
Nov 26, 2022
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
10 changes: 5 additions & 5 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ jobs:
virtualenv venv
source venv/bin/activate
pip install maturin toml pytest pytz pyarrow>=5.0
- name: Run Rust tests
run: |
source venv/bin/activate
cargo test -p arrow --test pyarrow --features pyarrow
- name: Run tests
env:
CARGO_HOME: "/home/runner/.cargo"
CARGO_TARGET_DIR: "/home/runner/target"
run: |
source venv/bin/activate
pushd arrow-pyarrow-integration-testing
cd arrow-pyarrow-integration-testing
maturin develop
pytest -v .
popd
4 changes: 4 additions & 0 deletions arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,7 @@ required-features = ["test_utils", "ipc"]
[[test]]
name = "csv"
required-features = ["csv", "chrono-tz"]

[[test]]
name = "pyarrow"
required-features = ["pyarrow"]
9 changes: 4 additions & 5 deletions arrow/src/pyarrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,19 @@ impl PyArrowConvert for RecordBatch {

fn to_pyarrow(&self, py: Python) -> PyResult<PyObject> {
let mut py_arrays = vec![];
let mut py_names = vec![];

let schema = self.schema();
let fields = schema.fields().iter();
let columns = self.columns().iter();

for (array, field) in columns.zip(fields) {
for array in columns {
py_arrays.push(array.data().to_pyarrow(py)?);
py_names.push(field.name());
}

let py_schema = schema.to_pyarrow(py)?;

let module = py.import("pyarrow")?;
let class = module.getattr("RecordBatch")?;
let record = class.call_method1("from_arrays", (py_arrays, py_names))?;
let record = class.call_method1("from_arrays", (py_arrays, py_schema))?;

Ok(PyObject::from(record))
}
Expand Down
42 changes: 42 additions & 0 deletions arrow/tests/pyarrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use arrow::array::{ArrayRef, Int32Array, StringArray};
use arrow::pyarrow::PyArrowConvert;
use arrow::record_batch::RecordBatch;
use pyo3::Python;
use std::sync::Arc;

#[test]
fn test_to_pyarrow() {
pyo3::prepare_freethreaded_python();

let a: ArrayRef = Arc::new(Int32Array::from(vec![1, 2]));
let b: ArrayRef = Arc::new(StringArray::from(vec!["a", "b"]));
let input = RecordBatch::try_from_iter(vec![("a", a), ("b", b)]).unwrap();
println!("input: {:?}", input);

let res = Python::with_gil(|py| {
let py_input = input.to_pyarrow(py)?;
let records = RecordBatch::from_pyarrow(py_input.as_ref(py))?;
let py_records = records.to_pyarrow(py)?;
RecordBatch::from_pyarrow(py_records.as_ref(py))
})
.unwrap();

assert_eq!(input, res);
}