{"record":{"id":"e5fa767d4ebb6109","repo":"pathwaycom/pathway","slug":"you-cannot-use-dep-to-column-expression-in-thi","errorCode":null,"errorMessage":"You cannot use {dep.to_column_expression()} in this reduce statement.\nMake 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()})","messagePattern":"You cannot use (.+?) in this reduce statement\\.\nMake sure that (.+?) is used in a groupby or wrap it with a reducer, e\\.g\\. pw\\.reducers\\.count\\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/groupbys.py","lineNumber":255,"sourceCode":"            self._maybe_warn(value)\n            column = self._eval(value, context)\n            reduced_columns[column_name] = column\n\n        result: table.Table = table.Table(\n            _columns=reduced_columns,\n            _context=context,\n        )\n        G.universe_solver.register_as_equal(self._universe, result._universe)\n        return result\n\n    def _validate_expression(self, expression: expr.ColumnExpression):\n        for dep in expression._dependencies_above_reducer():\n            if (\n                not isinstance(dep._table, thisclass.ThisMetaclass)  # allow for ix\n                and dep.to_column_expression()._to_original()._to_internal()\n                not in self._grouping_columns\n            ):\n                raise ValueError(\n                    f\"You cannot use {dep.to_column_expression()} in this reduce statement.\\n\"\n                    + f\"Make sure that {dep.to_column_expression()} is used in a groupby or wrap it with a reducer, \"\n                    + f\"e.g. pw.reducers.count({dep.to_column_expression()})\"\n                )\n\n        for dep in expression._dependencies_below_reducer():\n            if (\n                self._joinable_to_group._universe\n                != dep.to_column_expression()._column.universe\n            ):\n                raise ValueError(\n                    f\"You cannot use {dep.to_column_expression()} in this context.\"\n                    + \" Its universe is different than the universe of the table the method\"\n                    + \" was called on. You can use <table1>.with_universe_of(<table2>)\"\n                    + \" to assign universe of <table2> to <table1> if you're sure their\"\n                    + \" sets of keys are equal.\"\n                )\n","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/groupbys.py#L237-L273","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Wrap the bare column in a reducer, e.g. pw.reducers.latest(t.value) + 1 or pw.reducers.count(t.value)","Add the column to the groupby(...) call if it should be a grouping key: t.groupby(t.key, t.other)","Use pw.this columns consistently so the grouping-column membership check can match them"],"exampleFix":"# before\nres = t.groupby(t.key).reduce(t.key, total=t.value + 1)\n\n# after\nres = t.groupby(t.key).reduce(t.key, total=pw.reducers.sum(t.value) + 1)","handlingStrategy":"validation","validationCode":"def validate_reduce(grouped, expressions: dict):\n    grouping = {c.to_column_expression() for c in grouped._args}\n    # every positional/kwarg expression that is not a grouping column must go through a reducer\n    for name, e in expressions.items():\n        for dep in e._dependencies_above_reducer():\n            assert (\n                dep.to_column_expression()._to_original()._to_internal() in grouping\n            ), f\"wrap {name} in a reducer or group by it\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mentally map groupby->SQL GROUP BY: non-grouped columns only inside aggregate functions","Use pw.reducers.* explicitly; do not do arithmetic on bare columns inside reduce"],"tags":["pathway","groupby","reduce","validation","api-misuse"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}