polars_ops/frame/
gather.rs1use polars_core::prelude::*;
2
3use crate::series::convert_and_bound_index;
4
5pub trait GatherDf {
6 fn gather_with_column(&self, idxs: &Column, null_on_oob: bool) -> PolarsResult<DataFrame>;
8
9 fn gather_with_series(&self, idxs: &Series, null_on_oob: bool) -> PolarsResult<DataFrame>;
11}
12
13impl GatherDf for DataFrame {
14 fn gather_with_column(&self, idxs: &Column, null_on_oob: bool) -> PolarsResult<DataFrame> {
15 match idxs {
16 Column::Series(_) | Column::Dictionary(_) => {
17 self.gather_with_series(idxs.as_materialized_series(), null_on_oob)
18 },
19 Column::Scalar(idx_c) => {
20 if idx_c.is_empty() {
21 return Ok(self.clear());
22 }
23
24 let idx_s = idx_c.as_single_value_series();
25 let idx_ca = convert_and_bound_index(&idx_s, self.height(), null_on_oob)?;
26 match idx_ca.get(0) {
27 Some(idx) => Ok(self.new_from_index(idx as usize, idx_c.len())),
28 None => Ok(DataFrame::full_null(self.schema(), idx_c.len())),
29 }
30 },
31 }
32 }
33
34 fn gather_with_series(&self, idxs: &Series, null_on_oob: bool) -> PolarsResult<DataFrame> {
35 if idxs.is_empty() {
36 return Ok(self.clear());
37 }
38
39 let idx_ca = convert_and_bound_index(idxs, self.height(), null_on_oob)?;
40 Ok(unsafe { self.take_unchecked_impl(&idx_ca, false) })
41 }
42}