Compute new ids based on values in columns. Ids computed from `columns` must be row-wise unique. The uniqueness is not checked by pathway. Failing to provide unique ids can cause unexpected errors downstream. Args: columns: columns to be used as primary
(
self,
*args: expr.ColumnExpression | Value,
instance: expr.ColumnReference | None = None,
)
| 1940 | @desugar |
| 1941 | @check_arg_types |
| 1942 | def with_id_from( |
| 1943 | self, |
| 1944 | *args: expr.ColumnExpression | Value, |
| 1945 | instance: expr.ColumnReference | None = None, |
| 1946 | ) -> Table: |
| 1947 | """Compute new ids based on values in columns. |
| 1948 | Ids computed from `columns` must be row-wise unique. |
| 1949 | The uniqueness is not checked by pathway. Failing to provide unique ids can |
| 1950 | cause unexpected errors downstream. |
| 1951 | |
| 1952 | Args: |
| 1953 | columns: columns to be used as primary keys. |
| 1954 | |
| 1955 | Returns: |
| 1956 | Table: `self` updated with recomputed ids. |
| 1957 | |
| 1958 | Example: |
| 1959 | |
| 1960 | >>> import pathway as pw |
| 1961 | >>> t1 = pw.debug.table_from_markdown(''' |
| 1962 | ... | age | owner | pet |
| 1963 | ... 1 | 10 | Alice | 1 |
| 1964 | ... 2 | 9 | Bob | 1 |
| 1965 | ... 3 | 8 | Alice | 2 |
| 1966 | ... ''') |
| 1967 | >>> t2 = t1 + t1.select(old_id=t1.id) |
| 1968 | >>> t3 = t2.with_id_from(t2.age) |
| 1969 | >>> pw.debug.compute_and_print(t3) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1970 | | age | owner | pet | old_id |
| 1971 | ^... | 8 | Alice | 2 | ^... |
| 1972 | ^... | 9 | Bob | 1 | ^... |
| 1973 | ^... | 10 | Alice | 1 | ^... |
| 1974 | >>> t4 = t3.select(t3.age, t3.owner, t3.pet, same_as_old=(t3.id == t3.old_id), |
| 1975 | ... same_as_new=(t3.id == t3.pointer_from(t3.age))) |
| 1976 | >>> pw.debug.compute_and_print(t4) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1977 | | age | owner | pet | same_as_old | same_as_new |
| 1978 | ^... | 8 | Alice | 2 | False | True |
| 1979 | ^... | 9 | Bob | 1 | False | True |
| 1980 | ^... | 10 | Alice | 1 | False | True |
| 1981 | """ |
| 1982 | # new_index should be a column, so a little workaround |
| 1983 | new_index = self.select( |
| 1984 | ref_column=self.pointer_from(*args, instance=instance) |
| 1985 | ).ref_column |
| 1986 | |
| 1987 | return self._with_new_index( |
| 1988 | new_index=new_index, |
| 1989 | ) |
| 1990 | |
| 1991 | @trace_user_frame |
| 1992 | @contextualized_operator |