Performs a flatmap operation on a column or expression given as a first argument. Datatype of this column or expression has to be iterable or Json array. Other columns of the table are duplicated as many times as the length of the iterable. It is possible to get ids of sourc
(
self,
to_flatten: expr.ColumnReference,
*,
origin_id: str | None = None,
)
| 2339 | @desugar |
| 2340 | @check_arg_types |
| 2341 | def flatten( |
| 2342 | self, |
| 2343 | to_flatten: expr.ColumnReference, |
| 2344 | *, |
| 2345 | origin_id: str | None = None, |
| 2346 | ) -> Table: |
| 2347 | """Performs a flatmap operation on a column or expression given as a first |
| 2348 | argument. Datatype of this column or expression has to be iterable or Json array. |
| 2349 | Other columns of the table are duplicated as many times as the length of the iterable. |
| 2350 | |
| 2351 | It is possible to get ids of source rows by passing `origin_id` argument, which is |
| 2352 | a new name of the column with the source ids. |
| 2353 | |
| 2354 | Example: |
| 2355 | |
| 2356 | >>> import pathway as pw |
| 2357 | >>> t1 = pw.debug.table_from_markdown(''' |
| 2358 | ... | pet | age |
| 2359 | ... 1 | Dog | 2 |
| 2360 | ... 7 | Cat | 5 |
| 2361 | ... ''') |
| 2362 | >>> t2 = t1.flatten(t1.pet) |
| 2363 | >>> pw.debug.compute_and_print(t2, include_id=False) |
| 2364 | pet | age |
| 2365 | C | 5 |
| 2366 | D | 2 |
| 2367 | a | 5 |
| 2368 | g | 2 |
| 2369 | o | 2 |
| 2370 | t | 5 |
| 2371 | """ |
| 2372 | if origin_id is None: |
| 2373 | intermediate_table = self |
| 2374 | else: |
| 2375 | intermediate_table = self.with_columns(**{origin_id: thisclass.this.id}) |
| 2376 | |
| 2377 | return intermediate_table._flatten(to_flatten.name) |
| 2378 | |
| 2379 | @contextualized_operator |
| 2380 | def _flatten( |