From 9373fbfb94244e0dae3f0dab799ae900d5aa7fc0 Mon Sep 17 00:00:00 2001 From: ritchie Date: Mon, 25 Nov 2024 07:57:36 +0100 Subject: [PATCH] fix: Fix `gather_every` for `Scalar` --- crates/polars-core/src/frame/column/mod.rs | 5 ++++- py-polars/tests/unit/test_scalar.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/polars-core/src/frame/column/mod.rs b/crates/polars-core/src/frame/column/mod.rs index 70ce9b8855c7..585350c7627c 100644 --- a/crates/polars-core/src/frame/column/mod.rs +++ b/crates/polars-core/src/frame/column/mod.rs @@ -1101,7 +1101,10 @@ impl Column { match self { Column::Series(s) => s.gather_every(n, offset).into(), Column::Partitioned(s) => s.as_materialized_series().gather_every(n, offset).into(), - Column::Scalar(s) => s.resize(s.len() - offset / n).into(), + Column::Scalar(s) => { + let total = s.len() - offset; + s.resize(1 + (total - 1) / n).into() + }, } } diff --git a/py-polars/tests/unit/test_scalar.py b/py-polars/tests/unit/test_scalar.py index 6fa59a6c323d..4b1e9c135d5c 100644 --- a/py-polars/tests/unit/test_scalar.py +++ b/py-polars/tests/unit/test_scalar.py @@ -36,3 +36,14 @@ def test_null_literals(dtype: pl.DataType) -> None: .collect_schema() .dtypes() ) == [pl.Int64, dtype] + + +def test_scalar_19957() -> None: + value = 1 + values = [value] * 5 + foo = pl.DataFrame({"foo": values}) + foo_with_bar_from_literal = foo.with_columns(pl.lit(value).alias("bar")) + assert foo_with_bar_from_literal.gather_every(2).to_dict(as_series=False) == { + "foo": [1, 1, 1], + "bar": [1, 1, 1], + }