pub fn array2frequency2batch<T>(
arr: PrimitiveArray<T>,
sch: SchemaRef,
) -> Result<RecordBatch, Error>Expand description
Computes the frequencies of each value in a PrimitiveArray and converts the result into an Arrow RecordBatch.
§Arguments
sch: TheSchemaReffor the outputRecordBatch. This allows the caller to define custom column names and metadata for the resulting frequency and value columns. The schema is expected to have two fields, where the first field corresponds to the values from thePrimitiveArray(withT::DATA_TYPE) and the second field corresponds to the frequencies (withDataType::UInt64).
§Returns
A Result containing a RecordBatch with two columns, whose names are defined by the provided SchemaRef,
or an io::Error if an error occurs.
§Example
use std::sync::Arc;
use arrow::array::Int32Array;
use arrow::datatypes::{DataType, Field, Schema};
use rs_arrow_array2freq2rbat::array2frequency2batch;
let arr = Int32Array::from(vec![Some(2), Some(3), Some(3), Some(5), Some(5), Some(5)]);
let schema = Arc::new(Schema::new(vec![
Field::new("value", DataType::Int32, false),
Field::new("frequency", DataType::UInt64, false),
]));
let batch = array2frequency2batch(arr, schema).unwrap();
assert_eq!(batch.num_columns(), 2);
assert_eq!(batch.num_rows(), 3);