pathwaycom/pathway · error · ValueError

Table.__getitem__ argument has to be a ColumnReference to th

Error message

Table.__getitem__ argument has to be a ColumnReference to the same table or pw.this, or a string (or a list of those).

What it means

Table.__getitem__ accepts a ColumnReference, a string, or a list of those. If given a ColumnReference, it must point at this table (args.table is self) or be a pw.this-style reference (ThisMetaclass); otherwise ValueError. The guard prevents silently selecting a column that belongs to a different table, which would break universe tracking.

Source

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

        ... 10  | Alice | dog
        ... 9   | Bob   | dog
        ... 8   | Alice | cat
        ... 7   | Bob   | dog
        ... ''')
        >>> t2 = t1[["age", "pet"]]
        >>> t2 = t1[["age", t1.pet]]
        >>> pw.debug.compute_and_print(t2, include_id=False)
        age | pet
        7   | dog
        8   | cat
        9   | dog
        10  | dog
        """
        if isinstance(args, expr.ColumnReference):
            if (args.table is not self) and not isinstance(
                args.table, thisclass.ThisMetaclass
            ):
                raise ValueError(
                    "Table.__getitem__ argument has to be a ColumnReference to the same table or pw.this, or a string "
                    + "(or a list of those)."
                )
            return self._get_colref_by_name(args.name, KeyError)
        elif isinstance(args, str):
            return self._get_colref_by_name(args, KeyError)
        else:
            return self.select(*[self[name] for name in args])

    @staticmethod
    def _get_universe_solver() -> UniverseSolver:
        return G.universe_solver

    @trace_user_frame
    @staticmethod
    @check_arg_types
    def from_columns(
        *args: expr.ColumnReference, **kwargs: expr.ColumnReference

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Reference the column on the correct table: t1['col'] or t1.col
  2. If the column genuinely lives on another table, join the tables first, then select
  3. Use strings in list selections (t[['a','b']]) when mixing sources is not intended

Example fix

# before
sel = t1[t2.age]  # ValueError: reference belongs to t2

# after
sel = t1['age']  # or t1.age
# or, if the data is on t2:
joined = t1.join(t2, t1.id == t2.id)
sel = joined.select(joined.age)
Defensive patterns

Strategy: type-guard

Validate before calling

def ref_belongs_to(ref, table) -> bool:
    return ref.table is table

Type guard

import pathway as pw

def is_same_table_ref(ref, table: pw.Table) -> bool:
    return isinstance(ref, pw.ColumnReference) and ref.table is table

Prevention

When it happens

Trigger: t1[t2.col] where t2 is a different Table object (even one with an identically named column); passing a column reference captured from another table after refactor/rebinding; using a reference stored in a variable long after its table was transformed.

Common situations: Copy-pasting code where column refs come from mixed tables; passing t.other.col instead of t.col; interactive sessions where an old variable holds a stale reference.

Related errors


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