pub fn take<IndexType>(
values: &dyn Array,
indices: &PrimitiveArray<IndexType>,
options: Option<TakeOptions>
) -> Result<ArrayRef>where
IndexType: ArrowNumericType,
IndexType::Native: ToPrimitive,
Expand description
Take elements by index from Array, creating a new Array from those indexes.
┌─────────────────┐ ┌─────────┐ ┌─────────────────┐
│ A │ │ 0 │ │ A │
├─────────────────┤ ├─────────┤ ├─────────────────┤
│ D │ │ 2 │ │ B │
├─────────────────┤ ├─────────┤ take(values, indicies) ├─────────────────┤
│ B │ │ 3 │ ─────────────────────────▶ │ C │
├─────────────────┤ ├─────────┤ ├─────────────────┤
│ C │ │ 1 │ │ D │
├─────────────────┤ └─────────┘ └─────────────────┘
│ E │
└─────────────────┘
values array indicies array result
Errors
This function errors whenever:
- An index cannot be casted to
usize
(typically 32 bit architectures) - An index is out of bounds and
options
is set to check bounds.
Safety
When options
is not set to check bounds (default), taking indexes after len
is undefined behavior.
Examples
use arrow::array::{StringArray, UInt32Array};
use arrow::error::Result;
use arrow::compute::take;
let values = StringArray::from(vec!["zero", "one", "two"]);
// Take items at index 2, and 1:
let indices = UInt32Array::from(vec![2, 1]);
let taken = take(&values, &indices, None)?;
let taken = taken.as_any().downcast_ref::<StringArray>().unwrap();
assert_eq!(*taken, StringArray::from(vec!["two", "one"]));