1212from narwhals ._expression_parsing import ExprMetadata
1313from narwhals ._expression_parsing import apply_n_ary_operation
1414from narwhals ._expression_parsing import combine_metadata
15+ from narwhals ._expression_parsing import extract_compliant
1516from narwhals .dtypes import _validate_dtype
1617from narwhals .exceptions import LengthChangingExprError
1718from narwhals .expr_cat import ExprCatNamespace
@@ -1333,7 +1334,7 @@ def fill_null(
13331334 """Fill null values with given value.
13341335
13351336 Arguments:
1336- value: Value used to fill null values.
1337+ value: Value or expression used to fill null values.
13371338 strategy: Strategy used to fill null values.
13381339 limit: Number of consecutive null values to fill when using the 'forward' or 'backward' strategy.
13391340
@@ -1352,34 +1353,37 @@ def fill_null(
13521353 ... {
13531354 ... "a": [2, None, None, 3],
13541355 ... "b": [2.0, float("nan"), float("nan"), 3.0],
1356+ ... "c": [1, 2, 3, 4],
13551357 ... }
13561358 ... )
13571359 >>> df = nw.from_native(df_native)
13581360 >>> df.with_columns(
1359- ... nw.col("a", "b").fill_null(0).name.suffix("_nulls_filled")
1361+ ... nw.col("a", "b").fill_null(0).name.suffix("_filled"),
1362+ ... nw.col("a").fill_null(nw.col("c")).name.suffix("_filled_with_c"),
13601363 ... )
1361- ┌────────────────────────────────────────────────┐
1362- | Narwhals DataFrame |
1363- |------------------------------------------------|
1364- |shape: (4, 4) |
1365- |┌──────┬─────┬────────────────┬ ────────────────┐|
1366- |│ a ┆ b ┆ a_nulls_filled ┆ b_nulls_filled │|
1367- |│ --- ┆ --- ┆ --- ┆ --- │|
1368- |│ i64 ┆ f64 ┆ i64 ┆ f64 │|
1369- |╞══════╪═════╪════════════════╪ ════════════════╡|
1370- |│ 2 ┆ 2.0 ┆ 2 ┆ 2.0 │|
1371- |│ null ┆ NaN ┆ 0 ┆ NaN │|
1372- |│ null ┆ NaN ┆ 0 ┆ NaN │|
1373- |│ 3 ┆ 3.0 ┆ 3 ┆ 3.0 │|
1374- |└──────┴─────┴────────────────┴ ────────────────┘|
1375- └────────────────────────────────────────────────┘
1364+ ┌──────────────────────────────────────────────────────────── ┐
1365+ | Narwhals DataFrame |
1366+ |------------------------------------------------------------ |
1367+ |shape: (4, 6) |
1368+ |┌──────┬─────┬─────┬ ──────────┬──────────┬─ ────────────────┐|
1369+ |│ a ┆ b ┆ c ┆ a_filled ┆ b_filled ┆ a_filled_with_c │|
1370+ |│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │|
1371+ |│ i64 ┆ f64 ┆ i64 ┆ i64 ┆ f64 ┆ i64 │|
1372+ |╞══════╪═════╪═════╪ ══════════╪══════════╪═ ════════════════╡|
1373+ |│ 2 ┆ 2.0 ┆ 1 ┆ 2 ┆ 2.0 ┆ 2 │|
1374+ |│ null ┆ NaN ┆ 2 ┆ 0 ┆ NaN ┆ 2 │|
1375+ |│ null ┆ NaN ┆ 3 ┆ 0 ┆ NaN ┆ 3 │|
1376+ |│ 3 ┆ 3.0 ┆ 4 ┆ 3 ┆ 3.0 ┆ 3 │|
1377+ |└──────┴─────┴─────┴ ──────────┴──────────┴─ ────────────────┘|
1378+ └──────────────────────────────────────────────────────────── ┘
13761379
13771380 Using a strategy:
13781381
1379- >>> df.with_columns(
1382+ >>> df.select(
1383+ ... nw.col("a", "b"),
13801384 ... nw.col("a", "b")
13811385 ... .fill_null(strategy="forward", limit=1)
1382- ... .name.suffix("_nulls_forward_filled")
1386+ ... .name.suffix("_nulls_forward_filled"),
13831387 ... )
13841388 ┌────────────────────────────────────────────────────────────────┐
13851389 | Narwhals DataFrame |
@@ -1408,7 +1412,9 @@ def fill_null(
14081412 raise ValueError (msg )
14091413 return self ._from_callable (
14101414 lambda plx : self ._to_compliant_expr (plx ).fill_null (
1411- value = value , strategy = strategy , limit = limit
1415+ value = extract_compliant (plx , value , str_as_lit = True ),
1416+ strategy = strategy ,
1417+ limit = limit ,
14121418 )
14131419 )
14141420
0 commit comments