Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/polars-expr/src/dispatch/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ pub(super) fn slice(args: &mut [Column]) -> PolarsResult<Column> {
let offset_s = &args[1];
let length_s = &args[2];

let target_len = offset_s.len().max(length_s.len());
let list_ca = if list_ca.len() == 1 && target_len > 1 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be prohibitively expensive to be repeating the list values for each row; I'd have expected the implementation to have a kernel that can directly construct the output off of the single input row.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I will look into it! 👍

list_ca.new_from_index(0, target_len)
} else {
list_ca.clone()
};

let mut out: ListChunked = match (offset_s.len(), length_s.len()) {
(1, 1) => {
let offset = offset_s.get(0).unwrap().try_extract::<i64>()?;
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/operations/namespaces/list/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,20 @@ def test_list_eval_exceed_idx_size() -> None:
"len": [[2147483648], [2147483647], [2147483646]],
"unique": [[None], [None], [None]],
}


@pytest.mark.parametrize(
("offset", "length"),
[
(0, pl.lit(pl.Series([1, 2, 3]))),
(pl.lit(pl.Series([0, 1, 2])), 2),
(pl.lit(pl.Series([0, 1, 2])), pl.lit(pl.Series([3, 2, 1]))),
],
)
def test_list_slice_broadcast_27480(offset: Any, length: Any) -> None:
result = pl.select(pl.lit([0, 1, 2]).list.slice(offset, length).alias("broadcast"))
expected = pl.select(
pl.repeat(pl.lit([0, 1, 2]), 3).list.slice(offset, length).alias("broadcast")
)

assert_frame_equal(result, expected)
Loading