Reindexes the table using expression values as keys. Uses keys from context, or tries to infer proper context from the expression. If optional is True, then None in expression values result in None values in the result columns. Missing values in table keys result in RuntimeEr
(
self,
expression: expr.ColumnExpression,
*,
optional: bool = False,
context=None,
allow_misses: bool = False,
)
| 1414 | |
| 1415 | @trace_user_frame |
| 1416 | def ix( |
| 1417 | self, |
| 1418 | expression: expr.ColumnExpression, |
| 1419 | *, |
| 1420 | optional: bool = False, |
| 1421 | context=None, |
| 1422 | allow_misses: bool = False, |
| 1423 | ) -> Table: |
| 1424 | """Reindexes the table using expression values as keys. Uses keys from context, or tries to infer |
| 1425 | proper context from the expression. |
| 1426 | If optional is True, then None in expression values result in None values in the result columns. |
| 1427 | Missing values in table keys result in RuntimeError. |
| 1428 | If ``allow_misses`` is set to True, they result in None value on the output. |
| 1429 | |
| 1430 | Context can be anything that allows for `select` or `reduce`, or `pathway.this` construct |
| 1431 | (latter results in returning a delayed operation, and should be only used when using `ix` inside |
| 1432 | join().select() or groupby().reduce() sequence). |
| 1433 | |
| 1434 | Returns: |
| 1435 | Reindexed table with the same set of columns. |
| 1436 | |
| 1437 | Example: |
| 1438 | |
| 1439 | >>> import pathway as pw |
| 1440 | >>> t_animals = pw.debug.table_from_markdown(''' |
| 1441 | ... | epithet | genus |
| 1442 | ... 1 | upupa | epops |
| 1443 | ... 2 | acherontia | atropos |
| 1444 | ... 3 | bubo | scandiacus |
| 1445 | ... 4 | dynastes | hercules |
| 1446 | ... ''') |
| 1447 | >>> t_birds = pw.debug.table_from_markdown(''' |
| 1448 | ... | desc |
| 1449 | ... 2 | hoopoe |
| 1450 | ... 4 | owl |
| 1451 | ... ''') |
| 1452 | >>> ret = t_birds.select(t_birds.desc, latin=t_animals.ix(t_birds.id).genus) |
| 1453 | >>> pw.debug.compute_and_print(ret, include_id=False) |
| 1454 | desc | latin |
| 1455 | hoopoe | atropos |
| 1456 | owl | hercules |
| 1457 | """ |
| 1458 | |
| 1459 | if context is None: |
| 1460 | all_tables = collect_tables(expression) |
| 1461 | if len(all_tables) == 0: |
| 1462 | context = thisclass.this |
| 1463 | elif all(tab == all_tables[0] for tab in all_tables): |
| 1464 | context = all_tables[0] |
| 1465 | if context is None: |
| 1466 | for tab in all_tables: |
| 1467 | if not isinstance(tab, Table): |
| 1468 | raise ValueError("Table expected here.") |
| 1469 | if len(all_tables) == 0: |
| 1470 | raise ValueError("Const value provided.") |
| 1471 | context = all_tables[0] |
| 1472 | for tab in all_tables: |
| 1473 | assert context._universe.is_equal_to(tab._universe) |