pathwaycom/pathway · error · ValueError

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

Error message

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.

What it means

The second half of GroupBy._validate_expression: dependencies below a reducer must live on the same universe as the table the groupby was called on (self._joinable_to_group._universe). If a dep's column belongs to a different universe, ValueError explains the mismatch and points to <table1>.with_universe_of(<table2>) as the escape hatch for when key sets are provably equal.

Source

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

    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."
                )

    @lru_cache
    def _operator_dependencies(self) -> StableSet[table.Table]:
        # TODO + grouping columns expression dependencies
        return self._joinable_to_group._operator_dependencies()


class GroupedJoinResult(GroupedJoinable):
    _substitution_desugaring: SubstitutionDesugaring
    _groupby: GroupedTable

    def __init__(

View on GitHub (pinned to fa2f74a464)

Solutions

  1. If the two tables are guaranteed to have identical key sets, call t1.with_universe_of(t2) (or vice versa) before the groupby/reduce
  2. Otherwise perform an explicit join first so all referenced columns live on one universe
  3. Re-check that the referenced column really belongs to the table being grouped, not a leftover variable

Example fix

# before
res = t1.groupby(t1.key).reduce(t1.key, val=pw.reducers.sum(t2.value))

# after (keys provably equal)
t2_aligned = t2.with_universe_of(t1)
res = t1.groupby(t1.key).reduce(t1.key, val=pw.reducers.sum(t2_aligned.value))
Defensive patterns

Strategy: validation

Validate before calling

# before reduce, verify all referenced columns share the grouped table's universe
univ = table_to_group._universe
for col_expr in my_expressions:
    assert col_expr._column.universe == univ or col_expr._column.universe.is_subset_of(univ), "align universes with with_universe_of or join first"

Prevention

When it happens

Trigger: t.groupby(t.key).reduce(...) where the reducer argument or post-reducer expression references a column of a different table whose universe differs, e.g. mixing a column from t2 (built by a filter/join) into a reduce on t1 without universe alignment.

Common situations: Using ix/pointer lookups or join outputs inside reduce; combining columns from two tables that share keys by construction but not universe identity; migrating pandas merge-then-groupby code.

Related errors


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