Datasets:
Formats:
csv
Languages:
English
Size:
< 1K
Tags:
robotics
embodied-ai
human-demonstration
egocentric-video
bimanual-manipulation
tactile-sensing
License:
File size: 1,377 Bytes
9e18664 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from datafusion import DataFrame, Expr
def collect_to_string_list(df: DataFrame, col: str | Expr, remove_nulls: bool = True) -> list[str | None]:
"""
Collect a single column of a DataFrame into a Python string list.
This is a convenience function. DataFusion collection returns a stream
of record batches. Sometimes it is preferable to extract a single column
out of all of these batches and convert it to a string.
Parameters
----------
df:
The input DataFusion DataFrame
col:
The column to collect. You can provide either a string column
name or a DataFusion expression.
remove_nulls:
If true, any `null` values will be removed from the result. If false
these will be converted into None.
"""
batches = df.select(col).collect()
# Dataframe.collect() will return a list of record batches, but we only care
# about a single column. We want to combine the results of all batches into
# a single list. We also know from the above `select` that we will get a
# single column of data.
if remove_nulls:
return [str(r) for rss in batches for rs in rss for r in rs if r.is_valid]
return [str(r) if r.is_valid else None for rss in batches for rs in rss for r in rs]
|