Implementation:Online ml River Stream Iter Polars
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Data_Streaming, Data_Frames |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Converts Polars DataFrames into River-compatible data streams for online learning.
Description
The iter_polars function enables streaming over rows of Polars DataFrames, a fast DataFrame library for Python. It converts DataFrame rows into dictionary format suitable for River's online learning algorithms. The function supports both Series and DataFrame targets, including multi-output scenarios.
Usage
Use this when working with Polars DataFrames and want to apply River's online learning algorithms. It's particularly useful when you have data stored or processed in Polars format and need to convert it to River's streaming interface.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stream/iter_polars.py
Signature
def iter_polars(
X: pl.DataFrame,
y: pl.Series | pl.DataFrame | None = None,
**kwargs
) -> base.typing.Stream:
...
Import
from river import stream
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| X | pl.DataFrame | Polars DataFrame containing features |
| y | pl.Series, pl.DataFrame, or None | Target variable(s) as Series or DataFrame |
| **kwargs | dict | Additional arguments passed to stream.iter_array |
Returns:
| Type | Description |
|---|---|
| Iterator[(dict, Any)] | Stream of (features dict, target) tuples |
Usage Examples
import polars as pl
from river import stream
# Create a Polars DataFrame
X = pl.DataFrame({
'x1': [1, 2, 3, 4],
'x2': ['blue', 'yellow', 'yellow', 'blue'],
'y': [True, False, False, True]
})
# Extract target column
y = X.get_column('y')
X = X.drop("y")
# Iterate over the data
for features, target in stream.iter_polars(X, y):
print(f"Features: {features}")
print(f"Target: {target}")
print()
# Example with multi-output
X_multi = pl.DataFrame({
'feature1': [1.0, 2.0, 3.0],
'feature2': [4.0, 5.0, 6.0]
})
y_multi = pl.DataFrame({
'target1': [0.1, 0.2, 0.3],
'target2': [0.4, 0.5, 0.6]
})
print("Multi-output example:")
for x, y_dict in stream.iter_polars(X_multi, y_multi):
print(f"Features: {x}")
print(f"Targets: {y_dict}")
print()
# With shuffling (passed through kwargs)
shuffled_stream = stream.iter_polars(
X, y,
shuffle=True,
seed=42
)
print("Shuffled data:")
for x, y_val in shuffled_stream:
print(f"{x} -> {y_val}")