forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Impl and dispatch arr.first/last to get (pola-rs#13536)
- Loading branch information
Showing
14 changed files
with
381 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::array::{ArrayRef, FixedSizeListArray, PrimitiveArray}; | ||
use crate::legacy::compute::take::take_unchecked; | ||
use crate::legacy::prelude::*; | ||
use crate::legacy::utils::CustomIterTools; | ||
|
||
fn sub_fixed_size_list_get_indexes_literal(width: usize, len: usize, index: i64) -> IdxArr { | ||
(0..len) | ||
.map(|i| { | ||
if index >= width as i64 { | ||
return None; | ||
} | ||
|
||
index | ||
.negative_to_usize(width) | ||
.map(|idx| (idx + i * width) as IdxSize) | ||
}) | ||
.collect_trusted() | ||
} | ||
|
||
fn sub_fixed_size_list_get_indexes(width: usize, index: &PrimitiveArray<i64>) -> IdxArr { | ||
index | ||
.iter() | ||
.enumerate() | ||
.map(|(i, idx)| { | ||
if let Some(idx) = idx { | ||
if *idx >= width as i64 { | ||
return None; | ||
} | ||
|
||
idx.negative_to_usize(width) | ||
.map(|idx| (idx + i * width) as IdxSize) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect_trusted() | ||
} | ||
|
||
pub fn sub_fixed_size_list_get_literal(arr: &FixedSizeListArray, index: i64) -> ArrayRef { | ||
let take_by = sub_fixed_size_list_get_indexes_literal(arr.size(), arr.len(), index); | ||
let values = arr.values(); | ||
// Safety: | ||
// the indices we generate are in bounds | ||
unsafe { take_unchecked(&**values, &take_by) } | ||
} | ||
|
||
pub fn sub_fixed_size_list_get(arr: &FixedSizeListArray, index: &PrimitiveArray<i64>) -> ArrayRef { | ||
let take_by = sub_fixed_size_list_get_indexes(arr.size(), index); | ||
let values = arr.values(); | ||
// Safety: | ||
// the indices we generate are in bounds | ||
unsafe { take_unchecked(&**values, &take_by) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use arrow::legacy::kernels::fixed_size_list::{ | ||
sub_fixed_size_list_get, sub_fixed_size_list_get_literal, | ||
}; | ||
use polars_core::datatypes::ArrayChunked; | ||
use polars_core::prelude::arity::binary_to_series; | ||
|
||
use super::*; | ||
|
||
fn array_get_literal(ca: &ArrayChunked, idx: i64) -> PolarsResult<Series> { | ||
let chunks = ca | ||
.downcast_iter() | ||
.map(|arr| sub_fixed_size_list_get_literal(arr, idx)) | ||
.collect::<Vec<_>>(); | ||
Series::try_from((ca.name(), chunks)) | ||
.unwrap() | ||
.cast(&ca.inner_dtype()) | ||
} | ||
|
||
/// Get the value by literal index in the array. | ||
/// So index `0` would return the first item of every sub-array | ||
/// and index `-1` would return the last item of every sub-array | ||
/// if an index is out of bounds, it will return a `None`. | ||
pub fn array_get(ca: &ArrayChunked, index: &Int64Chunked) -> PolarsResult<Series> { | ||
match index.len() { | ||
1 => { | ||
let index = index.get(0); | ||
if let Some(index) = index { | ||
array_get_literal(ca, index) | ||
} else { | ||
polars_bail!(ComputeError: "unexpected null index received in `arr.get`") | ||
} | ||
}, | ||
len if len == ca.len() => { | ||
let out = binary_to_series(ca, index, |arr, idx| sub_fixed_size_list_get(arr, idx)); | ||
out?.cast(&ca.inner_dtype()) | ||
}, | ||
len => polars_bail!( | ||
ComputeError: | ||
"`arr.get` expression got an index array of length {} while the array has {} elements", | ||
len, ca.len() | ||
), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#[cfg(feature = "array_any_all")] | ||
mod any_all; | ||
mod get; | ||
mod min_max; | ||
mod namespace; | ||
mod sum_mean; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.