Flow
Flow[T] is the primary synchronous lazy pipeline for both ordinary values and
records. Transformations return a new pipeline; terminal methods execute its
plan.
Use flow(source) for an existing source and flow.defer(factory) when every
execution must create a fresh iterable. Besides ordinary iterables,
flow(source) retains supported PyArrow, pandas, Polars, Arrow C stream, and
dataframe-interchange inputs.
Record operations and the Rows view
Flow.rows() creates a lazy relational view without inspecting any item. The
view shares the same plan and source ownership, so it does not make a one-shot
input reusable.
Record operations whose names do not conflict with Flow semantics can be called
directly. select(), with_columns(), rename(), cast(), fill_nulls(),
drop_nulls(), explode(), unnest(), unpivot(), and pivot() return Rows;
group_by() returns GroupedRows.
Four relation-building names retain their established Flow meanings. Enter the Rows view first for these relational forms:
| Flow call | Flow meaning | Explicit Rows call | Rows meaning |
|---|---|---|---|
drop(count) |
Skip leading items | rows().drop(*columns) |
Remove record fields |
join(separator) |
Join string representations | rows().join(other, ...) |
Relational join |
aggregate(...) |
Execute and return a dictionary | rows().aggregate(...) |
Build a lazy one-row relation |
where(predicate) |
Alias of filter |
rows().where(predicate, **equalities) |
Filter records, including field equalities |
This table covers relation-building operations, not every shared method name.
Flow keeps its own output signatures too. Enter .rows() before to_csv(),
to_pandas(), or to_df() when you need Rows-specific writer or materializer
options.
Rows.map() and Rows.flat_map() return an ordinary Flow because their output
may have any shape. If those functions produce records, a later nonconflicting
record method can enter Rows again.
Tabular source routing
When the corresponding package is already loaded, flow(source) recognizes
concrete PyArrow Table, RecordBatch, and RecordBatchReader objects, pandas
DataFrames, and Polars DataFrame or LazyFrame objects. It does not import those
packages merely to probe an arbitrary object. A custom object with
__arrow_c_stream__ or __dataframe__ is routed through the matching adapter;
Arrow wins when both protocols are present. Pandas conversion emits data columns
only, not the dataframe index.
This routing does not sample ordinary iterable contents. Built-in containers, generators, NumPy arrays, and plain two-dimensional lists are not classified by their contents. Record methods still work when their items support the requested selectors. File paths are not guessed as CSV or Parquet inputs; use an explicit factory.
Conversion timing follows the selected protocol:
- an ordinary generator is not opened during construction;
- generic dataframe conversion and Polars LazyFrame collection are deferred until execution;
- a custom Arrow C stream is imported once during construction and is one-shot;
- a PyArrow
RecordBatchReaderis one-shot, while retained tables and record batches are reiterable.
Explaining terminal execution
explain() defaults to ordinary iteration. Pass a terminal name when you want
to inspect to_list(), count(), sum(), statistics, aggregation, or a
short-circuiting terminal. The same planner is used by the explanation and the
terminal itself.
from fpstreams import flow
explanation = flow([1, 2, 3]).explain(terminal="count").to_dict()
assert explanation["selected_engine"] == "python"
assert explanation["complexity"] == "O(1)"
assert explanation["semantics"]["output"]["cardinality"] == {
"kind": "exact",
"value": 3,
}
assert explanation["diagnostics"] == []
assert explanation["arrow_prefix"] is None
assert explanation["boundaries"] == []
An Arrow-capable plan reports its retained prefix and any guarded transition to
Python rows in arrow_prefix and boundaries. Relational plans additionally
report their selected tree and strategy in relations.
run_with_report() returns the terminal
value, its recorded route, and query-owned resource counts in one execution.
The report covers the outer plan and some direct paths; internal kernels and
fallbacks are not all recorded.
An identity list or tuple remains in Python under auto when a terminal would
otherwise scan and copy it. An identity range can still use native numeric
reduction. Exact-size count() is O(1) only for an unchanged, safely reiterable
source; operations and one-shot inputs are consumed normally.
CSV safety
Flow to_csv(..., spreadsheet_safe=False) writes arbitrary scalar, sequence, or
mapping items and accepts an optional header. Set spreadsheet_safe=True when
untrusted strings will be opened in Excel, Sheets, or similar software. Suspect
formula prefixes are neutralized with a leading single quote. The method writes
the file and returns None.
After entering Rows, to_csv() is the record writer instead and exposes
fieldnames, include_header, and extrasaction options.
Creating a flow
| Call | Behavior |
|---|---|
flow(source) |
Wrap an ordinary iterable, reuse a Flow/Rows plan, or route a supported tabular source |
flow.defer(factory) |
Call the factory for each execution |
flow.from_arrow(source) |
Adapt an Arrow table, batch, reader, or C stream provider |
flow.from_columns(columns) |
Build and retain an Arrow table from equal-length named columns |
flow.from_numpy(array, columns=...) |
Adapt a one-dimensional array to scalars or a two-dimensional array to named records |
flow.from_dataframe(frame) |
Use the dataframe interchange protocol; from_pandas is an alias |
flow.from_polars(frame) |
Retain a Polars DataFrame or LazyFrame |
flow.scan_csv(path) |
Scan typed Arrow CSV batches with query column pruning |
flow.from_parquet(source) |
Scan Parquet with optional projection and filtering |
flow.empty() |
Create a flow with no items |
flow.of_nullable(value) |
Emit one value, or nothing when it is None |
flow.iterate(seed, function) |
Repeatedly derive the next value from the previous one |
flow.generate(supplier) |
Call a supplier for every emitted value |
flow.concat(*sources) |
Read several sources in order |
Methods
fpstreams.Flow
Bases: FlowTerminalsMixin[T], Generic[T]
A synchronous pipeline that opens its source only when iterated or consumed.
run_with_report
Run one eager terminal normally and return its value with a read-only report.
terminal names an existing eager method such as "to_list" or
"sum". Lazy transformations and iteration are deliberately excluded.
to_list
Execute the pipeline and collect its items in a list.
Returns:
| Type | Description |
|---|---|
list[T]
|
All emitted items in encounter order. |
to_tuple
Execute the pipeline and collect its items in a tuple.
Returns:
| Type | Description |
|---|---|
tuple[T, ...]
|
All emitted items in encounter order as a tuple. |
to_set
Execute the pipeline and collect distinct hashable items.
Returns:
| Type | Description |
|---|---|
set[T]
|
The distinct emitted items; every item must be hashable. |
to_pandas
Execute the pipeline and build a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str] | None
|
Optional column labels passed to |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A pandas |
to_numpy
Execute the pipeline and build a NumPy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
Any
|
The optional NumPy data type used for the resulting array. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A NumPy array containing the emitted items. |
to_csv
to_csv(
path: str | PathLike[str],
*,
header: Iterable[str] | None = None,
encoding: str = "utf-8",
spreadsheet_safe: bool = False,
) -> None
Execute the pipeline and stream its items to a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file, opened in text write mode. |
required |
header
|
Iterable[str] | None
|
Optional header row. For mapping items, these names also select and order cells. |
None
|
encoding
|
str
|
Encoding used when opening the destination. |
'utf-8'
|
spreadsheet_safe
|
bool
|
Prefix formula-like string cells so spreadsheet software treats them as text. |
False
|
to_json
to_json(
path: str | PathLike[str],
*,
encoding: str = "utf-8",
ensure_ascii: bool = False,
default: Callable[[Any], Any] | None = None,
) -> None
Execute the pipeline and stream its items to one JSON array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file, replaced with one streamed JSON array. |
required |
encoding
|
str
|
Encoding used when opening the destination. |
'utf-8'
|
ensure_ascii
|
bool
|
Whether JSON output escapes non-ASCII characters. |
False
|
default
|
Callable[[Any], Any] | None
|
Optional serializer called for objects the JSON encoder cannot handle. |
None
|
describe
Return count and one-pass summary statistics for numeric items.
Returns:
| Type | Description |
|---|---|
dict[str, int | float]
|
A dictionary containing |
aggregate
Compute several named aggregations while traversing the flow once.
All named aggregators are updated during the same source traversal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**aggregations
|
Aggregator
|
Result names mapped to aggregators updated in one traversal. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Finished aggregation values keyed by the supplied argument names. |
collect
collect(
collector: Callable[[Iterable[T]], C]
| Collector[T, Any, C]
| None = None,
/,
**collectors: Collector[T, Any, Any],
) -> C | dict[str, Any]
Reduce the pipeline with one Collector or named Collectors.
One collector returns its finished value. Named collectors share one source traversal and return a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collector
|
Callable[[Iterable[T]], C] | Collector[T, Any, C] | None
|
One |
None
|
**collectors
|
Collector[T, Any, Any]
|
Named streaming collectors updated together in one traversal. |
{}
|
Returns:
| Type | Description |
|---|---|
C | dict[str, Any]
|
The collector result, or a dictionary of results for named collectors. |
join
Convert items to strings and join them with separator.
This is a string terminal operation; it consumes the flow and does not perform a relational join.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
separator
|
str
|
String placed between consecutive |
''
|
Returns:
| Type | Description |
|---|---|
str
|
One string containing every item separated by |
for_each
Execute action once for every item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Callable[[T], Any]
|
Called once for each emitted item; its return value is ignored. |
required |
partition
Collect matching and non-matching items in separate lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Called once per item; truthy items enter the first returned list. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[T], list[T]]
|
|
partition_results
Separate Result values into successes and failures.
Returns:
| Name | Type | Description |
|---|---|---|
success_values |
list[Any]
|
Unwrapped |
exceptions |
list[Exception]
|
Exceptions stored by |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any emitted item is neither |
to_async
View this synchronous pipeline as an AsyncFlow.
Returns:
| Type | Description |
|---|---|
Any
|
An |
first
Return the first item without consuming an unnecessary tail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Returned only when the flow is empty. |
_MISSING
|
Returns:
| Type | Description |
|---|---|
T | Any
|
The first item, or |
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If the flow is empty and no default is supplied. |
last
Return the last item, or default when the flow is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Returned only when the flow is empty. |
_MISSING
|
Returns:
| Type | Description |
|---|---|
T | Any
|
The last item, or |
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If the flow is empty and no default is supplied. |
find
Return the first matching item, a default, or raise EmptyFlowError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], Any]
|
Called in order until its first truthy result. |
required |
default
|
Any
|
Returned when no predicate result is truthy. |
_MISSING
|
Returns:
| Type | Description |
|---|---|
T | Any
|
The first matching item, or |
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If no item matches and no default is supplied. |
find_index
Return the index of the first matching item, or None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], Any]
|
Called with each item in order until its first truthy result. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The zero-based position of the first truthy predicate result, or |
index_of
Return the index of the first equal value, or None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
Target compared to each source item with equality. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
The zero-based position of the first item equal to |
nth
Return the item at a positive or negative index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Zero-based position; negative values count backward from the end. |
required |
default
|
Any
|
Returned when |
_MISSING
|
Returns:
| Type | Description |
|---|---|
T | Any
|
The selected item, or |
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If the index is out of range and no default is supplied. |
count
Count all items produced by the pipeline.
Returns:
| Type | Description |
|---|---|
int
|
The total number of emitted items. |
sum
Add all items, starting with start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
Any
|
Value added before all emitted items, matching Python's built-in |
0
|
Returns:
| Type | Description |
|---|---|
Any
|
The total of |
mean
Return the arithmetic mean, or None for an empty flow.
Returns:
| Type | Description |
|---|---|
float | None
|
The compensated floating-point mean, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an emitted item is not a real number. |
variance
Return the variance, or None when too few values are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ddof
|
int
|
Non-negative adjustment in the divisor |
1
|
Returns:
| Type | Description |
|---|---|
float | None
|
The floating-point variance, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an emitted item is not a real number. |
ValueError
|
If |
std
Return the standard deviation, or None when too few values are available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ddof
|
int
|
Non-negative adjustment in the variance divisor |
1
|
Returns:
| Type | Description |
|---|---|
float | None
|
The square root of the variance, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an emitted item is not a real number. |
ValueError
|
If |
min
Return the smallest item and raise on an empty flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], Any] | None
|
Optional callable whose result is compared instead of the item. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
The smallest item according to |
max
Return the largest item and raise on an empty flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], Any] | None
|
Optional callable whose result is compared instead of the item. |
None
|
Returns:
| Type | Description |
|---|---|
T
|
The largest item according to |
top
Return up to count largest items without sorting the entire result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of items to return. |
required |
key
|
Selector | None
|
Optional callable, field name, index, path, or expression used for ranking. |
None
|
Returns:
| Type | Description |
|---|---|
list[T]
|
Up to |
bottom
Return up to count smallest items without sorting the entire result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of items to return. |
required |
key
|
Selector | None
|
Optional callable, field name, index, path, or expression used for ranking. |
None
|
Returns:
| Type | Description |
|---|---|
list[T]
|
Up to |
minmax
Return the smallest and largest items in one traversal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Selector | None
|
Optional callable, field name, index, path, or expression used for comparison. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[T, T]
|
|
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If the flow emits no items. |
any
Return whether at least one item satisfies predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Tested in order until one result is truthy; defaults to |
bool
|
Returns:
| Type | Description |
|---|---|
bool
|
|
all
Return whether every item satisfies predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Tested in order until one result is falsey; defaults to |
bool
|
Returns:
| Type | Description |
|---|---|
bool
|
|
none
Return whether no item satisfies predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Tested in order until one result is truthy; defaults to |
bool
|
Returns:
| Type | Description |
|---|---|
bool
|
|
reduce
Combine items from left to right with an optional initial value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[Any, T], Any]
|
Called as |
required |
initial
|
Any
|
Starting accumulator; when omitted, the first item becomes the accumulator. |
_MISSING
|
Returns:
| Type | Description |
|---|---|
Any
|
The final left-to-right accumulator. |
Raises:
| Type | Description |
|---|---|
EmptyFlowError
|
If the flow is empty and |
reduce_right
reduce_right(
function: Callable[[T, Any], Any],
initial: Any = _MISSING,
*,
max_items: int | None = None,
) -> Any
Combine buffered items from right to left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T, Any], Any]
|
Called as |
required |
initial
|
Any
|
Starting accumulator; when omitted, the last item becomes the accumulator. |
_MISSING
|
max_items
|
int | None
|
Optional maximum number of source items that may be buffered. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The final right-to-left accumulator. |
Raises:
| Type | Description |
|---|---|
BufferLimitError
|
If the source contains more than |
EmptyFlowError
|
If the flow is empty and |
reduce_by
reduce_by(
key: Selector,
function: Callable[[R, T], R],
*,
initializer: Callable[[], R],
) -> dict[Any, R]
Reduce items independently for each selected key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Selector
|
Callable, field name, index, path, or expression selecting each group key. |
required |
function
|
Callable[[R, T], R]
|
Called as |
required |
initializer
|
Callable[[], R]
|
Called once when each distinct group is first encountered. |
required |
Returns:
| Type | Description |
|---|---|
dict[Any, R]
|
Final accumulator state for each hashable key, in first-key encounter order. |
frequencies
Count occurrences of values or selected keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Selector | None
|
Optional callable, field name, index, path, or expression selecting the value to count; the item itself is counted when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
dict[Any, int]
|
Occurrence counts keyed by each hashable selected value. |
of
staticmethod
Create a flow from positional items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
R
|
Positional values to emit in argument order. |
()
|
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A reusable flow that emits |
from_iterable
staticmethod
Create a flow over an iterable source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Iterable[R]
|
A synchronous iterable opened when the flow is consumed. Iterator instances remain one-shot; other iterables can be evaluated repeatedly. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow that emits each item from |
empty
staticmethod
Create a flow that emits no items.
Returns:
| Type | Description |
|---|---|
Flow[Any]
|
A reusable flow that always completes without emitting an item. |
of_nullable
staticmethod
Create an empty flow for None, otherwise emit the value once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
R | None
|
The optional item to emit; |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow containing |
iterate
staticmethod
Emit seed, then repeatedly apply function to the previous value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
R
|
The first value emitted or used to initialize the sequence. |
required |
function
|
Callable[[R], R]
|
Called with the previously emitted value to produce the next value. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A reusable, infinite flow beginning with |
generate
staticmethod
Create an infinite flow by calling supplier for each item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
supplier
|
Callable[[], R]
|
Called once for each requested item to produce that item. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A reusable, infinite flow of values returned by |
map
Apply function to each item lazily.
map is lazy: the callable runs only when a terminal operation or iteration consumes
the flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], R]
|
Receives each source item and returns its replacement value. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A lazy flow of mapped values. Any parallel settings on the current plan apply to this map. |
map_parallel
map_parallel(
function: Callable[[T], R],
*,
workers: int | None = None,
backend: ParallelBackend = "thread",
ordered: bool = True,
buffer: int | None = None,
) -> Flow[R]
Map items in a bounded thread or process pool.
Only a bounded number of tasks are submitted at once, preventing a slow consumer from creating unbounded work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], R]
|
Receives one source item in a worker and returns its mapped value. |
required |
workers
|
int | None
|
Worker count, or |
None
|
backend
|
ParallelBackend
|
Run callbacks in a |
'thread'
|
ordered
|
bool
|
Emit in source order when true, otherwise in completion order. |
True
|
buffer
|
int | None
|
Maximum submitted futures retained before the pipeline waits for a result. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow that submits bounded mapping work when consumed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If workers or buffer is less than one, or backend is unsupported. |
parallel
parallel(
*,
workers: int | None = None,
backend: ParallelBackend = "process",
ordered: bool = True,
buffer: int | None = None,
) -> Flow[T]
Apply parallel settings to map operations added after this call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workers
|
int | None
|
Worker count, or |
None
|
backend
|
ParallelBackend
|
Run callbacks in a |
'process'
|
ordered
|
bool
|
Preserve source order for subsequent maps when true. |
True
|
buffer
|
int | None
|
Maximum in-flight results retained by each subsequent map. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow sharing this pipeline with parallel defaults for maps appended afterward. |
sequential
Return a flow whose following maps run sequentially.
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow sharing this pipeline with parallel defaults cleared for later maps. |
tap
Run a side effect for each item while passing the item through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], None]
|
Called for its side effect before each original item is emitted. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow that emits every original item unchanged after calling |
filter
Keep items for which predicate returns a truthy value.
filter is lazy and preserves encounter order; the predicate runs as items are
requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], Any]
|
Called for each item; truthy results retain that item. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing only source items whose predicate result is truthy. |
reject
Drop items for which predicate returns a truthy value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], Any]
|
Called for each item; truthy results drop that item. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing only source items whose predicate result is falsey. |
compact
Drop None values, optionally selected from each item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Selector | None
|
Optional callable, field name, index, path, or expression whose selected
value is checked for |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow excluding items whose own or selected value is |
flat_map
Map each item to an iterable and emit the iterable contents.
Each input may emit zero or more output items. Nested iterables are consumed lazily in encounter order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], Iterable[R]]
|
Maps each source item to the iterable whose contents are emitted. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow that lazily emits every mapped iterable in source order. |
filter_map
Map items and discard results equal to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], R | None]
|
Maps each source item to one output value or |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow of non- |
pluck
Select one field, index, attribute, or nested path from each item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Selector
|
Callable, field name, index, path, or expression evaluated for each output. |
required |
Returns:
| Type | Description |
|---|---|
Flow[Any]
|
A flow containing the value selected from each source item. |
unique
Keep the first occurrence of each value in encounter order.
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing the first occurrence of each distinct value. |
distinct
Keep the first occurrence of each value in encounter order.
Returns:
| Type | Description |
|---|---|
Flow[T]
|
The same lazy de-duplication pipeline produced by |
unique_by
Keep the first item for each selected key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Selector
|
Callable, field name, index, path, or expression producing each uniqueness key. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing the first source item for each distinct selected key. |
sort_by
sort_by(
selector: Selector,
*,
reverse: bool = False,
buffer_size: int | None = None,
tempdir: str | PathLike[str] | None = None,
) -> Flow[T]
Sort items by a selector, optionally using bounded external storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Selector
|
Callable, field name, index, path, or expression producing comparison keys. |
required |
reverse
|
bool
|
Emit items in descending selected-key order when true. |
False
|
buffer_size
|
int | None
|
Items per sorted in-memory run; |
None
|
tempdir
|
str | PathLike[str] | None
|
Directory for temporary run files when |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow that emits every source item ordered by its selected value. |
sorted
sorted(
*,
key: Callable[[T], Any] | None = None,
reverse: bool = False,
buffer_size: int | None = None,
tempdir: str | PathLike[str] | None = None,
) -> Flow[T]
Sort the flow, optionally using bounded external runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], Any] | None
|
Optional callable used to derive each item's comparison key. |
None
|
reverse
|
bool
|
Emit items in descending comparison order when true. |
False
|
buffer_size
|
int | None
|
Items per sorted in-memory run; |
None
|
tempdir
|
str | PathLike[str] | None
|
Directory for temporary run files when |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow that globally orders the source by |
external_sort
external_sort(
*,
key: Callable[[T], Any] | None = None,
reverse: bool = False,
buffer_size: int = 100000,
tempdir: str | PathLike[str] | None = None,
) -> Flow[T]
Sort with a bounded in-memory buffer and temporary files.
Sorted runs are written to temporary files and merged lazily, keeping peak in-memory items bounded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], Any] | None
|
Optional callable used to derive each item's comparison key. |
None
|
reverse
|
bool
|
Emit items in descending comparison order when true. |
False
|
buffer_size
|
int
|
Maximum items sorted in memory for each temporary run. |
100000
|
tempdir
|
str | PathLike[str] | None
|
Directory in which temporary sorted runs are created. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A globally sorted flow produced by lazily merging bounded temporary runs. |
external_sort_by
external_sort_by(
selector: Selector,
*,
reverse: bool = False,
buffer_size: int = 100000,
tempdir: str | PathLike[str] | None = None,
) -> Flow[T]
Sort by a selector using bounded memory and temporary files.
Sorted runs are written to temporary files and merged lazily, keeping peak in-memory items bounded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Selector
|
Callable, field name, index, path, or expression producing comparison keys. |
required |
reverse
|
bool
|
Emit items in descending selected-key order when true. |
False
|
buffer_size
|
int
|
Maximum items sorted in memory for each temporary run. |
100000
|
tempdir
|
str | PathLike[str] | None
|
Directory in which temporary sorted runs are created. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow ordered by the selected value using bounded temporary runs. |
chunk
Group consecutive items into fixed-size tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Maximum number of consecutive items in each tuple. |
required |
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, ...]]
|
A flow of non-overlapping tuples, including a final shorter tuple when needed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If size is less than one. |
batch_by_size
batch_by_size(
max_size: int,
*,
max_count: int | None = None,
get_size: Callable[[T], int] = _default_item_size,
strict: bool = True,
) -> Flow[tuple[T, ...]]
Build batches constrained by item count and total measured size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum sum of item sizes in a normal batch. |
required |
max_count
|
int | None
|
Optional maximum item count per batch. |
None
|
get_size
|
Callable[[T], int]
|
Returns a non-negative integer size for each item; defaults to |
_default_item_size
|
strict
|
bool
|
Raise when one item exceeds |
True
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, ...]]
|
A flow of non-empty tuple batches within the size and count limits. With
|
window
Emit sliding tuples of size with the requested step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Number of items in each full window. |
required |
step
|
int
|
Number of source items consumed between successive windows. |
1
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, ...]]
|
A flow of full sliding windows. A non-empty source shorter than |
Raises:
| Type | Description |
|---|---|
ValueError
|
If size or step is less than one. |
group_runs
Group consecutive items that share the same key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Selector | None
|
Optional selector for run identity; adjacent items themselves are compared when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, ...]]
|
A flow of non-empty tuples, one for each contiguous run of equal keys. |
pairwise
Emit each adjacent pair of items.
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, T]]
|
A flow of overlapping |
pair_map
Apply a two-argument function to each adjacent pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T, T], R]
|
Called as |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow containing one mapped result per adjacent source pair. |
enumerate
Pair each item with a consecutive index starting at start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Integer index paired with the first source item. |
0
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[int, T]]
|
A flow of |
zip
Pair items with another iterable until one side ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Iterable[U]
|
Synchronous iterable providing the right-hand item in each pair. |
required |
strict
|
bool
|
Raise |
False
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, U]]
|
A flow of pairs ending with the shorter input unless |
zip_longest
Pair with another iterable until both sides end, filling missing values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Iterable[U]
|
Synchronous iterable providing right-hand values. |
required |
fillvalue
|
Any
|
Substitute used on whichever input is exhausted first. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T | Any, U | Any]]
|
A flow of pairs whose length matches the longer input. |
intersperse
Insert separator between consecutive items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
separator
|
T
|
Item emitted once between each pair of adjacent source items. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow with |
concat
Emit this flow followed by each supplied iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Iterable[T]
|
Synchronous iterables opened and emitted after this flow, in argument order. |
()
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow that drains this source and then each additional iterable in order. |
cross
Buffer another iterable once and emit a left-major Cartesian product.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Iterable[U]
|
Synchronous iterable buffered as the right side after the first left item. |
required |
max_right
|
int | None
|
Optional maximum number of right-side items that may be buffered. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[tuple[T, U]]
|
A flow of |
Raises:
| Type | Description |
|---|---|
BufferLimitError
|
During consumption if |
scan
Emit each left-to-right accumulator state after consuming one item.
Unlike reduce, scan emits every intermediate accumulator state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial
|
R
|
Accumulator passed to the first callback; it is not emitted by itself. |
required |
function
|
Callable[[R, T], R]
|
Called as |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow with one accumulated state for every source item. |
scan_right
scan_right(
initial: R,
function: Callable[[T, R], R],
*,
max_items: int | None = None,
) -> Flow[R]
Buffer the source, accumulate from right to left, and emit states in source order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial
|
R
|
Accumulator passed to the rightmost callback; it is not emitted by itself. |
required |
function
|
Callable[[T, R], R]
|
Called as |
required |
max_items
|
int | None
|
Optional maximum number of source items that may be buffered. |
None
|
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow with one right-fold state per source item, ordered like the original items. |
Raises:
| Type | Description |
|---|---|
BufferLimitError
|
During consumption if the source exceeds |
gather
Apply a stateful Gatherer that may emit zero or more values per item.
A gatherer may retain state and emit zero, one, or many outputs for each input item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gatherer
|
Gatherer[T, Any, R]
|
The stateful gatherer applied to this pipeline. |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow of values emitted by |
fold
Consume all items into one emitted value using fresh state per iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initializer
|
Callable[[], R]
|
Called once per evaluation to create the initial accumulator. |
required |
function
|
Callable[[R, T], R]
|
Called as |
required |
Returns:
| Type | Description |
|---|---|
Flow[R]
|
A flow that emits exactly one final accumulator, including for an empty source. |
prepend
Emit values before the items from this flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
T
|
Items to emit before the first source item, in argument order. |
()
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing |
append
Emit values after the items from this flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
T
|
Items to emit after the source completes, in argument order. |
()
|
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing every source item followed by |
map_first
Transform only the first item, if one exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], T]
|
Maps the first item; it is never called for an empty source. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow with only its first item replaced by |
map_last
Transform only the last item, if one exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[[T], T]
|
Maps the final item; it is never called for an empty source. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow with only its final item replaced by |
collapse
Merge adjacent items while collapsible returns true.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collapsible
|
Callable[[T, T], bool]
|
Called on neighboring original items to decide whether a run continues. |
required |
merger
|
Callable[[T, T], T]
|
Combines the current run aggregate with the next item. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing one merged aggregate for each contiguous collapsible run. |
attempt
with_engine
Request automatic, Python, or native execution for this plan.
Selecting an engine changes execution policy without consuming the source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Engine
|
The execution engine requested for this pipeline. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
An equivalent lazy flow whose plan requests |
explain
Describe engine selection, stages, and fused operations without executing.
This inspection method does not consume or execute the source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminal
|
TerminalName
|
Terminal operation to include when validating and selecting the engine. |
'iterate'
|
Returns:
| Type | Description |
|---|---|
PlanExplanation
|
A structured explanation of the selected engine and planned stages. |
pairs
View a flow of two-tuples as a key/value Pairs pipeline.
Returns:
| Type | Description |
|---|---|
Any
|
A lazy |
rows
View this flow as a lazy record pipeline without inspecting its items.
Returns:
| Type | Description |
|---|---|
Rows[T]
|
A Rows view that shares this flow's plan and source ownership. |
select
Project record fields through a lazy Rows view of this flow.
with_columns
Add or replace record fields through a lazy Rows view of this flow.
rename
Rename record fields through a lazy Rows view of this flow.
cast
Convert named record fields through a lazy Rows view of this flow.
fill_nulls
Replace missing or None record fields through a lazy Rows view.
drop_nulls
Drop records according to selected None values through a Rows view.
explode
explode(
selector: Selector,
*,
into: str | None = None,
outer: bool = False,
) -> fpstreams.Rows[dict[str, Any]]
Expand one selected iterable through a lazy Rows view.
unnest
Promote fields from one nested record through a lazy Rows view.
unpivot
unpivot(
*columns: str,
names_to: str = "variable",
values_to: str = "value",
) -> fpstreams.Rows[dict[str, Any]]
Reshape wide records into name/value records through a Rows view.
pivot
pivot(
*,
index: Selector | tuple[Selector, ...],
columns: Selector,
values: Selector,
aggregate: str | Callable[[Any, Any], Any] = "error",
fill: Any = None,
) -> fpstreams.Rows[dict[str, Any]]
Reshape long records into wide records through a lazy Rows view.
group_by
Describe grouped aggregation through a lazy Rows view of this flow.
take
Emit at most count items, then close the upstream iterator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of leading items to emit. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing only the first |
Raises:
| Type | Description |
|---|---|
ValueError
|
If count is negative. |
limit
Emit at most count items; alias of take.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Maximum number of leading items to emit. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
The same bounded pipeline produced by |
drop
Skip count items before yielding the remainder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of leading items to consume without emitting. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow containing every source item after the first |
Raises:
| Type | Description |
|---|---|
ValueError
|
If count is negative. |
skip
Skip count items; alias of drop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of leading items to consume without emitting. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
The same suffix pipeline produced by |
take_while
Emit the longest prefix that satisfies predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Called on leading items until its first falsey result. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow ending before the first source item whose predicate result is falsey. |
take_while_inclusive
Emit through the first item that fails predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Called on leading items through its first falsey result. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow ending after emitting the first item whose predicate result is falsey. |
drop_while
Skip the longest prefix that satisfies predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
Called only while leading items produce truthy results. |
required |
Returns:
| Type | Description |
|---|---|
Flow[T]
|
A flow starting with the first item for which |