Restrict self universe to keys appearing in other. Args: other: table which universe is used to restrict universe of self. Returns: Table: table with restricted universe, with the same set of columns Example: >>> import pathway as pw
(self, other: TableLike)
| 1085 | @trace_user_frame |
| 1086 | @check_arg_types |
| 1087 | def restrict(self, other: TableLike) -> Table[TSchema]: |
| 1088 | """Restrict self universe to keys appearing in other. |
| 1089 | |
| 1090 | Args: |
| 1091 | other: table which universe is used to restrict universe of self. |
| 1092 | |
| 1093 | Returns: |
| 1094 | Table: table with restricted universe, with the same set of columns |
| 1095 | |
| 1096 | |
| 1097 | Example: |
| 1098 | |
| 1099 | >>> import pathway as pw |
| 1100 | >>> t1 = pw.debug.table_from_markdown( |
| 1101 | ... ''' |
| 1102 | ... | age | owner | pet |
| 1103 | ... 1 | 10 | Alice | 1 |
| 1104 | ... 2 | 9 | Bob | 1 |
| 1105 | ... 3 | 8 | Alice | 2 |
| 1106 | ... ''' |
| 1107 | ... ) |
| 1108 | >>> t2 = pw.debug.table_from_markdown( |
| 1109 | ... ''' |
| 1110 | ... | cost |
| 1111 | ... 2 | 100 |
| 1112 | ... 3 | 200 |
| 1113 | ... ''' |
| 1114 | ... ) |
| 1115 | >>> t2.promise_universe_is_subset_of(t1) |
| 1116 | <pathway.Table schema={'cost': <class 'int'>}> |
| 1117 | >>> t3 = t1.restrict(t2) |
| 1118 | >>> pw.debug.compute_and_print(t3, include_id=False) |
| 1119 | age | owner | pet |
| 1120 | 8 | Alice | 2 |
| 1121 | 9 | Bob | 1 |
| 1122 | """ |
| 1123 | if self._universe == other._universe: |
| 1124 | warnings.warn("Identical universes for Table.restrict().", stacklevel=5) |
| 1125 | return self |
| 1126 | if not other._universe.is_subset_of(self._universe): |
| 1127 | raise ValueError( |
| 1128 | "Table.restrict(): other universe has to be a subset of self universe." |
| 1129 | + "Consider using Table.promise_universe_is_subset_of() to assert it." |
| 1130 | ) |
| 1131 | if other._universe.is_equal_to(self._universe): |
| 1132 | warnings.warn( |
| 1133 | "Unnecessary call to Table.restrict(), consider using Table.with_universe_of().", |
| 1134 | stacklevel=5, |
| 1135 | ) |
| 1136 | return self._restrict(other) |
| 1137 | |
| 1138 | @contextualized_operator |
| 1139 | def _restrict(self, other: TableLike) -> Table[TSchema]: |