Method: Polars::DataFrame#map_rows
- Defined in:
- lib/polars/data_frame.rb
#map_rows(return_dtype: nil, inference_size: 256, &f) ⇒ Object Also known as: apply
Note:
The frame-level apply cannot track column names (as the UDF is a black-box
that may arbitrarily drop, rearrange, transform, or add new columns); if you
want to apply a UDF such that column names are preserved, you should use the
expression-level apply syntax instead.
Apply a custom/user-defined function (UDF) over the rows of the DataFrame.
The UDF will receive each row as a tuple of values: udf(row).
Implementing logic using a Ruby function is almost always significantly slower and more memory intensive than implementing the same logic using the native expression API because:
- The native expression engine runs in Rust; UDFs run in Ruby.
- Use of Ruby UDFs forces the DataFrame to be materialized in memory.
- Polars-native expressions can be parallelised (UDFs cannot).
- Polars-native expressions can be logically optimised (UDFs cannot).
Wherever possible you should strongly prefer the native expression API to achieve the best performance.
3413 3414 3415 3416 3417 3418 3419 3420 |
# File 'lib/polars/data_frame.rb', line 3413 def map_rows(return_dtype: nil, inference_size: 256, &f) out, is_df = _df.map_rows(f, return_dtype, inference_size) if is_df _from_rbdf(out) else _from_rbdf(Utils.wrap_s(out).to_frame._df) end end |