Skip to content

Commit

Permalink
fix(rust, python): include offset in arr.get (#5193)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 13, 2022
1 parent 7d45ba1 commit 1e03e52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions polars/polars-arrow/src/kernels/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ use crate::utils::CustomIterTools;
///
/// ```
fn sublist_get_indexes(arr: &ListArray<i64>, index: i64) -> IdxArr {
let mut iter = arr.offsets().iter();
let offsets = arr.offsets().as_slice();
let mut iter = offsets.iter();

let mut cum_offset: IdxSize = 0;
// the indices can be sliced, so we should not start at 0.
let mut cum_offset = (*offsets.first().unwrap_or(&0)) as IdxSize;

if let Some(mut previous) = iter.next().copied() {
let a: IdxArr = iter
Expand Down
29 changes: 29 additions & 0 deletions py-polars/tests/unit/test_lists.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import typing
from datetime import date, datetime, time

import numpy as np
import pandas as pd

import polars as pl
Expand Down Expand Up @@ -505,3 +507,30 @@ def test_list_slice() -> None:
assert df.select([pl.col("lst").arr.slice(-2, "len")]).to_dict(False) == {
"lst": [[3, 4], [2, 1]]
}


@typing.no_type_check
def test_list_sliced_get_5186() -> None:
n = 30
df = pl.from_dict(
{
"ind": pl.arange(0, n, eager=True),
"inds": np.stack([np.arange(n), -np.arange(n)], axis=-1),
}
)

assert df.select(
[
"ind",
pl.col("inds").arr.first().alias("first_element"),
pl.col("inds").arr.last().alias("last_element"),
]
)[10:20].frame_equal(
df[10:20].select(
[
"ind",
pl.col("inds").arr.first().alias("first_element"),
pl.col("inds").arr.last().alias("last_element"),
]
)
)

0 comments on commit 1e03e52

Please sign in to comment.