pola-rs/polars · error · TypeError

`arctan2` expected a `str` or `Expr` got a `{qualified_type_

Error message

`arctan2` expected a `str` or `Expr` got a `{qualified_type_name(y)}`

What it means

Same validation as the x argument: pl.arctan2(y, x) requires y to be a str column name or an Expr. The y check runs after x, so a bad y raises only once x is acceptable. Numeric literals, Series, and None in the y position produce this TypeError.

Source

Thrown at py-polars/src/polars/functions/lazy.py:1763

    │ ---       ┆ ---       ┆ ---       │
    │ f64       ┆ f64       ┆ f64       │
    ╞═══════════╪═══════════╪═══════════╡
    │ 0.707107  ┆ 0.707107  ┆ 0.785398  │
    │ -0.707107 ┆ 0.707107  ┆ -0.785398 │
    │ 0.707107  ┆ -0.707107 ┆ 2.356194  │
    │ -0.707107 ┆ -0.707107 ┆ -2.356194 │
    └───────────┴───────────┴───────────┘
    """
    if isinstance(y, str):
        y = F.col(y)
    if isinstance(x, str):
        x = F.col(x)
    if not hasattr(x, "_pyexpr"):
        msg = f"`arctan2` expected a `str` or `Expr` got a `{qualified_type_name(x)}`"
        raise TypeError(msg)
    if not hasattr(y, "_pyexpr"):
        msg = f"`arctan2` expected a `str` or `Expr` got a `{qualified_type_name(y)}`"
        raise TypeError(msg)

    return wrap_expr(plr.arctan2(y._pyexpr, x._pyexpr))


@deprecated("`arctan2d` is deprecated; use `arctan2` followed by `.degrees()` instead.")
def arctan2d(y: str | Expr, x: str | Expr) -> Expr:
    """
    Compute two argument arctan in degrees.

    .. deprecated:: 1.0.0
        Use `arctan2` followed by :meth:`Expr.degrees` instead.

    Returns the angle (in degrees) in the plane between the positive x-axis
    and the ray from the origin to (x,y).

    Parameters
    ----------
    y

View on GitHub (pinned to df599052da)

Solutions

  1. Wrap constants: pl.arctan2(pl.lit(1.0), pl.col('x'))
  2. Use column names (str) for frame columns, or build Exprs explicitly
  3. Move Series data into a DataFrame and select with named columns

Example fix

# before
pl.arctan2(1.0, pl.col('x'))  # TypeError on y

# after
pl.arctan2(pl.lit(1.0), pl.col('x'))
Defensive patterns

Strategy: validation

Validate before calling

def to_operand(v):
    if isinstance(v, str):
        return pl.col(v)
    if not hasattr(v, '_pyexpr'):
        return pl.lit(v)
    return v

expr = pl.arctan2(to_operand(y), to_operand(x))

Type guard

def is_expr_or_str(v) -> bool:
    return isinstance(v, (str, pl.Expr))

Prevention

When it happens

Trigger: pl.arctan2(1.0, pl.col('x')); pl.arctan2(s_y_series, 'x'); pl.arctan2(None, 'x'). Argument-order mistakes often put the literal in y expecting numpy's (y, x) convention — which is in fact correct here, but both slots must be expressions.

Common situations: Porting math.atan2 / numpy.arctan2 calls with scalar operands; passing precomputed Series; forgetting pl.lit for constants.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/73def16501c9a74b. Report an issue: GitHub.