Filter a table according to `filter_expression` condition. Args: filter_expression: `ColumnExpression` that specifies the filtering condition. Returns: Table: Result has the same schema as `self` and its ids are subset of `self.id`. Example:
(self, filter_expression: expr.ColumnExpression)
| 495 | @desugar |
| 496 | @check_arg_types |
| 497 | def filter(self, filter_expression: expr.ColumnExpression) -> Table[TSchema]: |
| 498 | """Filter a table according to `filter_expression` condition. |
| 499 | |
| 500 | |
| 501 | Args: |
| 502 | filter_expression: `ColumnExpression` that specifies the filtering condition. |
| 503 | |
| 504 | Returns: |
| 505 | Table: Result has the same schema as `self` and its ids are subset of `self.id`. |
| 506 | |
| 507 | |
| 508 | Example: |
| 509 | |
| 510 | >>> import pathway as pw |
| 511 | >>> vertices = pw.debug.table_from_markdown(''' |
| 512 | ... label outdegree |
| 513 | ... 1 3 |
| 514 | ... 7 0 |
| 515 | ... ''') |
| 516 | >>> filtered = vertices.filter(vertices.outdegree == 0) |
| 517 | >>> pw.debug.compute_and_print(filtered, include_id=False) |
| 518 | label | outdegree |
| 519 | 7 | 0 |
| 520 | """ |
| 521 | filter_type = self.eval_type(filter_expression) |
| 522 | if filter_type != dt.BOOL: |
| 523 | raise TypeError( |
| 524 | f"Filter argument of Table.filter() has to be bool, found {filter_type}." |
| 525 | ) |
| 526 | ret = self._filter(filter_expression) |
| 527 | if ( |
| 528 | filter_col := expr.get_column_filtered_by_is_none(filter_expression) |
| 529 | ) is not None and filter_col.table == self: |
| 530 | name = filter_col.name |
| 531 | dtype = self._columns[name].dtype |
| 532 | ret = ret.update_types(**{name: dt.unoptionalize(dtype)}) |
| 533 | return ret |
| 534 | |
| 535 | @trace_user_frame |
| 536 | @desugar |