Skip to content

Commit 9565988

Browse files
feat(python,rust): allow extend_constant to work with date literals (#6114)
1 parent c8d45ae commit 9565988

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

polars/polars-core/src/series/ops/extend.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ impl Series {
1313
Int64(v) => Series::new("", vec![v]),
1414
Utf8(v) => Series::new("", vec![v]),
1515
Boolean(v) => Series::new("", vec![v]),
16+
Date(v) => Series::new("", vec![v]),
1617
Null => BooleanChunked::full_null("", 1).into_series(),
1718
dt => panic!("{dt:?} not supported"),
1819
};

py-polars/polars/internals/expr/expr.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,17 +5649,19 @@ def ewm_var(
56495649
alpha = _prepare_alpha(com, span, half_life, alpha)
56505650
return wrap_expr(self._pyexpr.ewm_var(alpha, adjust, bias, min_periods))
56515651

5652-
def extend_constant(self, value: int | float | str | bool | None, n: int) -> Expr:
5652+
def extend_constant(
5653+
self, value: int | float | str | bool | date | None, n: int
5654+
) -> Expr:
56535655
"""
56545656
Extend the Series with given number of values.
56555657
56565658
Parameters
56575659
----------
56585660
value
5659-
The value to extend the Series with. This value may be None to fill with
5660-
nulls.
5661+
A constant literal value (not an expression) with which to extend the
5662+
Series; can pass None to fill the Series with nulls.
56615663
n
5662-
The number of values to extend.
5664+
The number of additional values that will be added into the Series.
56635665
56645666
Examples
56655667
--------
@@ -5679,6 +5681,8 @@ def extend_constant(self, value: int | float | str | bool | None, n: int) -> Exp
56795681
└────────┘
56805682
56815683
"""
5684+
if isinstance(value, Expr):
5685+
raise TypeError(f"'value' must be a supported literal; found {value!r}")
56825686
return wrap_expr(self._pyexpr.extend_constant(value, n))
56835687

56845688
def value_counts(self, multithreaded: bool = False, sort: bool = False) -> Expr:

py-polars/polars/internals/series/series.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,17 +4804,19 @@ def ewm_var(
48044804
48054805
"""
48064806

4807-
def extend_constant(self, value: int | float | str | bool | None, n: int) -> Series:
4807+
def extend_constant(
4808+
self, value: int | float | str | bool | date | None, n: int
4809+
) -> Series:
48084810
"""
48094811
Extend the Series with given number of values.
48104812
48114813
Parameters
48124814
----------
48134815
value
4814-
The value to extend the Series with. This value may be None to fill with
4815-
nulls.
4816+
A constant literal value (not an expression) with which to extend the
4817+
Series; can pass None to fill the Series with nulls.
48164818
n
4817-
The number of values to extend.
4819+
The number of additional values that will be added into the Series.
48184820
48194821
Examples
48204822
--------

py-polars/tests/unit/test_series.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,12 +2240,17 @@ def test_ewm_param_validation() -> None:
22402240

22412241

22422242
def test_extend_constant() -> None:
2243-
a = pl.Series("a", [1, 2, 3])
2244-
expected = pl.Series("a", [1, 2, 3, 1, 1, 1])
2245-
verify_series_and_expr_api(a, expected, "extend_constant", 1, 3)
2243+
today = date.today()
22462244

2247-
expected = pl.Series("a", [1, 2, 3, None, None, None])
2248-
verify_series_and_expr_api(a, expected, "extend_constant", None, 3)
2245+
for const, dtype in (
2246+
(1, pl.Int8),
2247+
(today, pl.Date),
2248+
("xyz", pl.Utf8),
2249+
(None, pl.Float64),
2250+
):
2251+
s = pl.Series("s", [None], dtype=dtype)
2252+
expected = pl.Series("s", [None, const, const, const], dtype=dtype)
2253+
verify_series_and_expr_api(s, expected, "extend_constant", const, 3)
22492254

22502255

22512256
def test_any_all() -> None:

0 commit comments

Comments
 (0)