pathwaycom/pathway · error · KeyError

Column name {repr(old)} not found in a {self}.

Error message

Column name {repr(old)} not found in a {self}.

What it means

Raised by TableSlice.rename() when a key of rename_dict does not match any column in the slice. rename() normalizes both keys and values (strings or ColumnReferences) and then verifies every old name exists in the slice mapping before rebuilding it.

Source

Thrown at python/pathway/internals/table_slice.py:103

            if colname not in mapping:
                raise KeyError(f"Column name {repr(colname)} not found in a {self}.")
            mapping.pop(colname)
        return TableSlice(mapping, self._table)

    @trace_user_frame
    @check_arg_types
    def rename(
        self,
        rename_dict: dict[str | ColumnReference, str | ColumnReference],
    ) -> TableSlice:
        rename_dict_normalized = {
            self._normalize(old): self._normalize(new)
            for old, new in rename_dict.items()
        }
        mapping = self._mapping.copy()
        for old in rename_dict_normalized.keys():
            if old not in mapping:
                raise KeyError(f"Column name {repr(old)} not found in a {self}.")
            mapping.pop(old)
        for old, new in rename_dict_normalized.items():
            mapping[new] = self._mapping[old]
        return TableSlice(mapping, self._table)

    @trace_user_frame
    @check_arg_types
    def with_prefix(self, prefix: str) -> TableSlice:
        return self.rename({name: prefix + name for name in self.keys()})

    @trace_user_frame
    @check_arg_types
    def with_suffix(self, suffix: str) -> TableSlice:
        return self.rename({name: name + suffix for name in self.keys()})

    @trace_user_frame
    def ix(self, expression, *, optional: bool = False, context=None) -> TableSlice:
        new_table = self._table.ix(expression, optional=optional, context=context)

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Print list(table_slice.keys()) and make every key of rename_dict one of those names
  2. If the column exists on the base table, do the rename on the table before select(), or select the column into the slice first
  3. Guard programmatically: only rename entries whose key is present: table_slice.rename({k: v for k, v in rename_dict.items() if k in table_slice.keys()})
  4. Check for earlier rename/without calls in the chain that changed the column set

Example fix

# before
slice = table.select("a", "b").rename({"c": "c2"})  # KeyError

# after
slice = table.rename({"c": "c2"}).select("a", "b")
# or simply
slice = table.select("a", "b").rename({"a": "a2"})
Defensive patterns

Strategy: validation

Validate before calling

rename_dict = {"x": "y"}
known = set(table_slice.keys())
bad_keys = [k for k in rename_dict if k not in known]
assert not bad_keys, f"rename keys not in slice: {bad_keys}"

Type guard

def rename_keys_valid(slice_: TableSlice, rename_dict: dict) -> bool:
    return all(k in slice_.keys() for k in rename_dict)

Try / catch

try:
    slice2 = table_slice.rename(rename_dict)
except KeyError:
    valid = {k: v for k, v in rename_dict.items() if k in table_slice.keys()}
    slice2 = table_slice.rename(valid)

Prevention

When it happens

Trigger: Calling table.select('a','b').rename({'x': 'y'}) where 'x' is not a selected column; renaming with pw.this.x or table.x references for a column not in the slice; renaming a column that an earlier without() already removed.

Common situations: Copy-pasting a rename dictionary from a longer table onto a projected slice; typos in old names; renaming after the column was dropped or renamed earlier in the chain.

Related errors


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