Function arrow_select::take::take

source ·
pub fn take(
    values: &dyn Array,
    indices: &dyn Array,
    options: Option<TakeOptions>
) -> Result<ArrayRef, ArrowError>
Expand description

Take elements by index from Array, creating a new Array from those indexes.

┌─────────────────┐      ┌─────────┐                              ┌─────────────────┐
│        A        │      │    0    │                              │        A        │
├─────────────────┤      ├─────────┤                              ├─────────────────┤
│        D        │      │    2    │                              │        B        │
├─────────────────┤      ├─────────┤   take(values, indices)      ├─────────────────┤
│        B        │      │    3    │ ─────────────────────────▶   │        C        │
├─────────────────┤      ├─────────┤                              ├─────────────────┤
│        C        │      │    1    │                              │        D        │
├─────────────────┤      └─────────┘                              └─────────────────┘
│        E        │
└─────────────────┘
   values array          indices array                              result

For selecting values by index from multiple arrays see crate::interleave

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, taking indexes after len will panic.

Examples

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).unwrap();
let taken = taken.as_string::<i32>();

assert_eq!(*taken, StringArray::from(vec!["two", "one"]));