pola-rs/polars · error · TypeError

"pad_end" expects a `str`, given a

Error message

"pad_end" expects a `str`, given a {qualified_type_name(fill_char)!r}

What it means

Expr.str.pad_end() requires `fill_char` to be a str; any other type raises this TypeError with the qualified type name of what was given. Like pad_start, the fill character is validated client-side before building the expression.

Solutions

  1. Pass a single-character string, e.g. fill_char=' '.
  2. Convert the value: fill_char=str(value) if it is a scalar char.
  3. Guard optional values with a default: fill_char or ' '.

Example fix

// before
pl.col('name').str.pad_end(20, '-')
pl.col('name').str.pad_end(20, 0)
// after
pl.col('name').str.pad_end(20, '0')
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(fill_char, str) or len(fill_char) != 1:
    raise ValueError('fill_char must be a single-character string')

Type guard

def is_fill_char(v) -> bool:
    return isinstance(v, str) and len(v) == 1

Try / catch

try:
    expr = pl.col('s').str.pad_end(n, fill_char)
except TypeError:
    expr = pl.col('s').str.pad_end(n, ' ')

Prevention

When it happens

Trigger: Calling Expr.str.pad_end(length, fill_char=0), fill_char=None, or a collection such as fill_char=['.'] .

Common situations: Numeric fill values from config; None defaults leaking in; copying pad_start code and swapping the argument order so a non-str lands in fill_char.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/aacc10b76bb875f0. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/expr/string.py:935

        >>> df.with_columns(padded=pl.col("a").str.pad_end(8, "*"))
        shape: (4, 2)
        ┌──────────────┬──────────────┐
        │ a            ┆ padded       │
        │ ---          ┆ ---          │
        │ str          ┆ str          │
        ╞══════════════╪══════════════╡
        │ cow          ┆ cow*****     │
        │ monkey       ┆ monkey**     │
        │ hippopotamus ┆ hippopotamus │
        │ null         ┆ null         │
        └──────────────┴──────────────┘
        """
        length_pyexpr = parse_into_expression(length)
        if not isinstance(fill_char, str):
            msg = (
                f'"pad_end" expects a `str`, given a {qualified_type_name(fill_char)!r}'
            )
            raise TypeError(msg)
        return wrap_expr(self._pyexpr.str_pad_end(length_pyexpr, fill_char))

    def zfill(self, length: int | IntoExprColumn) -> Expr:
        """
        Pad the start of the string with zeros until it reaches the given length.

        A sign prefix (`-`) is handled by inserting the padding after the sign
        character rather than before.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        length
            Pad the string until it reaches this length. Strings with length equal to
            or greater than this value are returned as-is.

        See Also

View on GitHub (pinned to fe841f959e)