pathwaycom/pathway · error · ValueError

You cannot use {dep.to_column_expression()} in this reduce s

Error message

You cannot use {dep.to_column_expression()} in this reduce statement.
Make sure that {dep.to_column_expression()} is used in a groupby or wrap it with a reducer, e.g. pw.reducers.count({dep.to_column_expression()})

What it means

In a groupby(...).reduce(...) call, every column referenced above a reducer must either be one of the grouping columns or be wrapped in a reducer. _validate_expression walks _dependencies_above_reducer() and raises ValueError naming the offending column expression, suggesting pw.reducers.count() as an example wrapper.

Source

Thrown at python/pathway/internals/groupbys.py:255

            self._maybe_warn(value)
            column = self._eval(value, context)
            reduced_columns[column_name] = column

        result: table.Table = table.Table(
            _columns=reduced_columns,
            _context=context,
        )
        G.universe_solver.register_as_equal(self._universe, result._universe)
        return result

    def _validate_expression(self, expression: expr.ColumnExpression):
        for dep in expression._dependencies_above_reducer():
            if (
                not isinstance(dep._table, thisclass.ThisMetaclass)  # allow for ix
                and dep.to_column_expression()._to_original()._to_internal()
                not in self._grouping_columns
            ):
                raise ValueError(
                    f"You cannot use {dep.to_column_expression()} in this reduce statement.\n"
                    + f"Make sure that {dep.to_column_expression()} is used in a groupby or wrap it with a reducer, "
                    + f"e.g. pw.reducers.count({dep.to_column_expression()})"
                )

        for dep in expression._dependencies_below_reducer():
            if (
                self._joinable_to_group._universe
                != dep.to_column_expression()._column.universe
            ):
                raise ValueError(
                    f"You cannot use {dep.to_column_expression()} in this context."
                    + " Its universe is different than the universe of the table the method"
                    + " was called on. You can use <table1>.with_universe_of(<table2>)"
                    + " to assign universe of <table2> to <table1> if you're sure their"
                    + " sets of keys are equal."
                )

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Wrap the bare column in a reducer, e.g. pw.reducers.latest(t.value) + 1 or pw.reducers.count(t.value)
  2. Add the column to the groupby(...) call if it should be a grouping key: t.groupby(t.key, t.other)
  3. Use pw.this columns consistently so the grouping-column membership check can match them

Example fix

# before
res = t.groupby(t.key).reduce(t.key, total=t.value + 1)

# after
res = t.groupby(t.key).reduce(t.key, total=pw.reducers.sum(t.value) + 1)
Defensive patterns

Strategy: validation

Validate before calling

def validate_reduce(grouped, expressions: dict):
    grouping = {c.to_column_expression() for c in grouped._args}
    # every positional/kwarg expression that is not a grouping column must go through a reducer
    for name, e in expressions.items():
        for dep in e._dependencies_above_reducer():
            assert (
                dep.to_column_expression()._to_original()._to_internal() in grouping
            ), f"wrap {name} in a reducer or group by it"

Prevention

When it happens

Trigger: t.groupby(t.key).reduce(out=t.value + 1) where t.value is not in the groupby list and is used outside a reducer; also referencing a non-grouped column inside an arithmetic expression passed to reduce.

Common situations: SQL habits: SELECT key, value + 1 FROM t GROUP BY key is invalid in SQL too, but pandas groupby users expect it; forgetting to add a column to groupby(); composing reducer output with raw columns (count() + t.value).

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/e5fa767d4ebb6109. Report an issue: GitHub.