nxcals.api.custom.data.services.DataFrame.mapInArrow
- DataFrame.mapInArrow(func: ArrowMapIterFunction, schema: StructType | str, barrier: bool = False, profile: ResourceProfile | None = None) DataFrame
Maps an iterator of batches in the current
DataFrameusing a Python native function that is performed on pyarrow.RecordBatchs both as input and output, and returns the result as aDataFrame.This method applies the specified Python function to an iterator of pyarrow.RecordBatchs, each representing a batch of rows from the original DataFrame. The returned iterator of pyarrow.RecordBatchs are combined as a
DataFrame. The size of the function’s input and output can be different. Each pyarrow.RecordBatch size can be controlled by spark.sql.execution.arrow.maxRecordsPerBatch.Added in version 3.3.0.
- Parameters:
func (function) – a Python native function that takes an iterator of pyarrow.RecordBatchs, and outputs an iterator of pyarrow.RecordBatchs.
schema (
pyspark.sql.types.DataTypeor str) – the return type of the func in PySpark. The value can be either apyspark.sql.types.DataTypeobject or a DDL-formatted type string.barrier (bool, optional, default False) –
Use barrier mode execution, ensuring that all Python workers in the stage will be launched concurrently.
profile (
pyspark.resource.ResourceProfile. The optional ResourceProfile) –to be used for mapInArrow.
Examples
>>> import pyarrow as pa >>> df = spark.createDataFrame([(1, 21), (2, 30)], ("id", "age")) >>> def filter_func(iterator): ... for batch in iterator: ... pdf = batch.to_pandas() ... yield pa.RecordBatch.from_pandas(pdf[pdf.id == 1]) >>> df.mapInArrow(filter_func, df.schema).show() +---+---+ | id|age| +---+---+ | 1| 21| +---+---+
Set
barriertoTrueto force themapInArrowstage running in the barrier mode, it ensures all Python workers in the stage will be launched concurrently.>>> df.mapInArrow(filter_func, df.schema, barrier=True).collect() [Row(id=1, age=21)]
See also
pyspark.sql.functions.pandas_udf,DataFrame.mapInPandas