Pandas Window TDS Frame
Represents a window specification over a base frame.
Created by expanding(), rolling(), or window_frame_legend_ext()
on a PandasApiTdsFrame
or a
PandasApiGroupbyTdsFrame.
Calling an aggregate (e.g. .aggregate('sum')) on this object produces
a new TDS frame whose columns contain the windowed values. The
first(), last(), and window_extend_legend_ext() methods
are also available for positional and custom window operations.
Alternatively, use bracket notation to select a single column first
and obtain a
WindowSeries.
Obtaining a PandasApiWindowTdsFrame
# Expanding (cumulative) window — all rows up to current row
window = frame.expanding(order_by="col")
# Fixed-size sliding window — 5 preceding rows to current row
window = frame.rolling(5, order_by="col")
# Custom window bounds (pylegend extension)
window = frame.window_frame_legend_ext(
frame_spec=frame.rows_between(-3, 3),
order_by="col",
)
# No frame clause — only PARTITION BY / ORDER BY
window = frame.window_frame_legend_ext(
frame_spec=None,
order_by="col",
)
When created from a groupby, the grouping columns are automatically
used as PARTITION BY in the generated SQL:
window = frame.groupby("grp").expanding(order_by="val")
# SQL: ... PARTITION BY "grp", ... ORDER BY "val" ...
Selecting a single column
Use bracket notation to narrow to one column before aggregating.
This returns a
WindowSeries
whose aggregate result can be assigned back to the parent frame:
frame["cumsum"] = frame.expanding(order_by="col")["col"].sum()
Order-by resolution
If no order_by is supplied when creating the window, the first
column of the base frame is used as a fallback. Providing an
explicit order_by is recommended for deterministic results.
- param base_frame:
The underlying frame or groupby frame.
- param order_by:
Column name(s) to use for
ORDER BYwithin the window.Nonemeans no explicit ordering (the first column of the base frame is used as a fallback).- type order_by:
str, list of str, or None
- param frame_spec:
A
FrameSpecdescribing the window frame bounds. Defaults toRowsBetween(None, None)(i.e.ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING). PassNoneto omit the frame clause entirely, producing a window with onlyPARTITION BY/ORDER BY. This is required for functions likeshift()(lag/lead) which do not accept a frame clause.- type frame_spec:
RowsBetween, RangeBetween, or None
- param ascending:
Sort direction(s) for the
ORDER BYcolumns.Truemeans ascending. Can be a singlebool(applied to all columns) or alist[bool]whose length must match the number oforder_bycolumns.- type ascending:
bool or list of bool, default True
See also
PandasApiTdsFrame.expandingCreate an expanding window.
PandasApiTdsFrame.rollingCreate a rolling window.
PandasApiTdsFrame.window_frame_legend_extCreate a custom window.
WindowSeriesSingle-column proxy on a window frame.
firstFirst value in the window for every column.
lastLast value in the window for every column.
window_extend_legend_extCustom single-column window function.
Notes
Differences from pandas:
In pandas,
ExpandingandRollingobjects have built-in convenience methods (sum(),mean(), etc.) that return aDataFrame. Here, the window frame object exposes onlyaggregate()/agg()for multi-column use. For single-column convenience methods (sum(),mean(), etc.), use bracket notation to get aWindowSeriesfirst.When created from a groupby, the grouping columns are excluded from the result columns (only the aggregated value columns appear).
The
order_byparameter is a pylegend extension. In pandas, window ordering relies on the implicit order of the DataFrame index.Extra
*args/**kwargsonaggregate()are not supported.
Examples
Download Interactive Notebook
agg
- PandasApiWindowTdsFrame.agg(func, axis=0, *args, **kwargs)[source]
Apply a window aggregate function across all non-grouping columns.
Alias for
aggregate(). Seeaggregatefor full documentation.See also
aggregateEquivalent method (canonical name).
- Return type:
PandasApiBaseTdsFrame
aggregate
- PandasApiWindowTdsFrame.aggregate(func, axis=0, *args, **kwargs)[source]
Apply a window aggregate function across all non-grouping columns.
Compute the window aggregate specified by
funcover the window defined by thisPandasApiWindowTdsFrame. The result is a new TDS frame whose columns contain the windowed values.- Parameters:
func (
Union[Callable[...,Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]],str,ufunc,List[Union[Callable[...,Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]],str,ufunc]],Mapping[Hashable,Union[Callable[...,Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]],str,ufunc,List[Union[Callable[...,Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]],str,ufunc]]]]]) –Aggregation specification. Accepted forms:
str— a named aggregation (e.g.'sum','mean','min','max','count','std','var').callable— a function receiving a column proxy and returning an aggregated value.list— a list of the above.dict— a mapping of column name → aggregation(s).
axis (
Union[int,str]) – Only0/'index'is supported.*args (
Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]) – Not supported.**kwargs (
Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]) – Not supported.
- Returns:
A new TDS frame with the windowed aggregate values.
- Return type:
PandasApiBaseTdsFrame
See also
aggAlias for
aggregate.PandasApiTdsFrame.aggregateFrame-level aggregation (no window).
PandasApiGroupbyTdsFrame.aggregateGrouped aggregation.
Notes
Differences from pandas:
In pandas,
Expanding.aggregate()andRolling.aggregate()accept*argsand**kwargsforwarded to the aggregation function. Here, extra positional and keyword arguments are not supported.The result is a full TDS frame (all non-grouping columns are aggregated), not a single column. Use bracket notation on the window frame to select a single column before aggregating (returns a
WindowSeries).
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Expanding sum over all numeric columns frame.filter( items=["Order Id"] ).expanding(order_by="Order Id").aggregate("sum").head(5).to_pandas()
Order Id 0 10248 1 20497 2 30747 3 40998 4 51250 # Rolling mean with a window of 3 frame.filter( items=["Order Id"] ).rolling(3, order_by="Order Id").aggregate("mean").head(5).to_pandas()
Order Id 0 10248.0 1 10248.5 2 10249.0 3 10250.0 4 10251.0
first
- PandasApiWindowTdsFrame.first(numeric_only=False)[source]
Return the first value in the window for every column.
Generates
first_value(col) OVER (...)for each column.- Parameters:
numeric_only (
bool) – IfTrue, applyfirst_valueonly to numeric columns and retain grouping columns. Non-numeric, non-grouping columns are dropped from the result.- Returns:
A new frame whose columns contain the first value within the window.
- Return type:
PandasApiTdsFrame
See also
lastLast value in the window.
WindowSeries.firstSingle-column version.
Notes
Differences from pandas:
first()is a pylegend extension. In pandas, there is noExpanding.first()orRolling.first().When
numeric_only=True, the implementation appliesfirst()per numeric column individually, then filters.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# First Order Id across the entire sorted window frame.filter(items=["Order Id"]).window_frame_legend_ext( frame_spec=frame.rows_between(), order_by="Order Id", ).first().head(5).to_pandas()
Order Id 0 10248 1 10248 2 10248 3 10248 4 10248
last
- PandasApiWindowTdsFrame.last(numeric_only=False)[source]
Return the last value in the window for every column.
Generates
last_value(col) OVER (...)for each column.- Parameters:
numeric_only (
bool) – IfTrue, applylast_valueonly to numeric columns and retain grouping columns. Non-numeric, non-grouping columns are dropped from the result.- Returns:
A new frame whose columns contain the last value within the window.
- Return type:
PandasApiTdsFrame
See also
firstFirst value in the window.
WindowSeries.lastSingle-column version.
Notes
Differences from pandas:
last()is a pylegend extension. In pandas, there is noExpanding.last()orRolling.last().When
numeric_only=True, the implementation applieslast()per numeric column individually, then filters.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Last Order Id across the entire sorted window frame.filter(items=["Order Id"]).window_frame_legend_ext( frame_spec=frame.rows_between(), order_by="Order Id", ).last().head(5).to_pandas()
Order Id 0 11077 1 11077 2 11077 3 11077 4 11077
window_extend_legend_ext
- PandasApiWindowTdsFrame.window_extend_legend_ext(value_func, agg_func=None)[source]
Apply a custom window function to all columns in the frame.
PyLegend extension — not present in pandas.
Compute a user-defined window expression over every non-grouping column. The
value_funcreceives three arguments — aPandasApiPartialFrame(p), aPandasApiWindowReference(w), and aPandasApiTdsRow(r) — and must return either a single primitive (processed per-column) or aPandasApiTdsRow(expanded to all columns).- Parameters:
value_func (
Callable[[PandasApiPartialFrame,PyLegendWindowReference,PandasApiTdsRow],Union[int,float,str,bool,date,datetime,Decimal,PyLegendPrimitive]]) –(p, w, r) -> primitive | PandasApiTdsRow.Common patterns:
lambda p, w, r: p.first(w, r)— first value (returnsPandasApiTdsRow→ applies to all columns).lambda p, w, r: p.last(w, r)— last value.lambda p, w, r: p.nth(w, r, 3)— nth value.lambda p, w, r: p.lag(r, 1)— lag (previous row).lambda p, w, r: p.lead(r, 2)— lead (future row).lambda p, w, r: r["col"]— raw column ref (combined withagg_func).
agg_func (
Optional[Callable[[PyLegendPrimitiveCollection],PyLegendPrimitive]]) –(collection) -> primitive. If provided, an additional aggregation step (e.g.lambda c: c.sum()) is applied on top of thevalue_funcresult.
- Returns:
A new TDS frame with the window function applied to every column.
- Return type:
PandasApiBaseTdsFrame
See also
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# nth-value across all columns frame.filter(items=["Order Id"]).window_frame_legend_ext( frame_spec=frame.rows_between(), order_by="Order Id", ).window_extend_legend_ext( value_func=lambda p, w, r: p.nth(w, r, 3), ).head(5).to_pandas()
Order Id 0 10250 1 10250 2 10250 3 10250 4 10250