Pandas Series
A single-column proxy for a PandasApiTdsFrame.
A Series is conceptually similar to a pandas.Series: it
represents one column of a frame and supports element-wise
arithmetic, string methods, date-part extraction, and other
transformations.
Obtaining a Series
Use bracket notation on a PandasApiTdsFrame.
The returned subclass matches the column type.
For example, an integer column becomes an IntegerSeries.
Transformations
A Series supports the same operator overloads as the
underlying primitive type. For example, an IntegerSeries
supports +, -, *, /, %, comparisons, etc.
A StringSeries supports .upper(), .lower(),
.len(), .startswith(), .contains(),
.replace(), concatenation with +, etc.
A DateTimeSeries supports .year(), .month(),
.day(), etc.
Transforming a Series produces a new Series with
an updated expression tree — the original column is never
mutated.
Assigning back to the frame
Use bracket assignment (__setitem__) to write a Series
back into the frame — either overwriting an existing column or
creating a new one.
Constants (int, float, str, bool, date,
datetime) and callables (lambda) are also accepted on the
right-hand side.
Important
A Series can only be assigned to the same frame it was
derived from. Assigning a Series from a different frame
raises ValueError.
Window functions on a Series
Certain window functions such as rank() can be called on a
Series. The result is a new Series whose values are the
window-function output for that column.
Series with applied functions (aggregations or window functions) can also be combined with arithmetic in the same assignment, but only one function call is allowed per expression. If multiple function calls are needed, split them into separate steps.
See also
PandasApiTdsFrameThe parent frame class.
PandasApiGroupbyTdsFrameGroupby object (returns
GroupbySerieswhen bracket-indexed).
Notes
Differences from pandas:
A
Seriesis not a first-class data container. It is an expression builder that lazily constructs the query. No data is materialised untilexecute_frame_to_string()orto_pandas()is called.Cross-frame assignment is not allowed. In pandas you can freely assign a Series from one DataFrame to another (alignment happens on the index); here the Series must originate from the same frame instance. If you need cross-frame assignment, use join or merge.
Applying a function on a computed series expression is not supported in certain cases. For example,
(frame['col'] + 5).rank()raisesNotImplementedError. Instead, doframe['col'].rank() + 5.
Examples
Download Interactive Notebook
agg
- Series.agg(func, axis=0, *args, **kwargs)[source]
Alias for
aggregate().See
aggregate()for full documentation.- Return type:
Union[PandasApiTdsFrame,Series]
aggregate
- Series.aggregate(func, axis=0, *args, **kwargs)[source]
Aggregate the Series using one or more operations.
Reduce the single column to one or more scalar values. The result is returned as a single-row
PandasApiTdsFrame.- 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:
str — a named aggregation (
'sum','mean','min','max','count','std','var', plus aliases'len','size').callable — a lambda receiving the Series and calling one of its aggregation methods (e.g.
lambda x: x.sum()).list of str — multiple named aggregations. Result columns are named
"agg(col_name)".dict —
{column_name: agg_spec}. Keys must match the Series’ column name.
axis (
Union[int,str]) – Must be0or'index'.
- Returns:
A single-row frame with the aggregated value(s).
- Return type:
Union[PandasApiTdsFrame,Series]- Raises:
NotImplementedError – If called on a computed Series expression (e.g.
(frame['col'] + 5).aggregate('sum')). Assign the expression to a column first, then aggregate.ValueError – If a dict key does not match the Series’ column name.
Notes
Differences from pandas:
In pandas,
Series.aggregatecan return a scalar, a Series, or a DataFrame depending on the input. Here the result is always a single-rowPandasApiTdsFrame.Aggregation on a computed Series expression is not supported. Assign the expression to the frame first.
When
funcis a dict, keys must exactly match the Series’ column name — no other column names are valid.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Single named aggregation frame["Order Id"].aggregate("sum").to_pandas()
Order Id 0 8849875 # Multiple aggregations via a list frame["Order Id"].aggregate(["sum", "min", "max"]).to_pandas()
sum(Order Id) min(Order Id) max(Order Id) 0 8849875 10248 11077 # Lambda aggregation frame["Order Id"].aggregate(lambda x: x.count()).to_pandas()
Order Id 0 830
concat_legend_ext
- Series.concat_legend_ext(other)[source]
Concatenate this series with another series vertically.
PyLegend extension — not present in pandas.
Performs a
UNION ALLof this series withother. Both series must have compatible schemas (same column name and type).- Parameters:
other (
Series) – Another series with the same column name and type.- Returns:
A new series containing rows from both series.
- Return type:
Series- Raises:
ValueError – If the schemas of the two series are incompatible.
Notes
Differences from pandas:
In pandas,
pd.concatis a top-level function. Here,concat_legend_extis a method on aSeriesand only supports vertical concatenation (UNION ALL) of two single-column series with the same schema.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
s1 = frame.head(3)["Order Id"] s2 = frame.head(3)["Order Id"] s1.concat_legend_ext(s2).to_pandas()
Order Id 0 10248 1 10249 2 10250 3 10248 4 10249 5 10250
count
- Series.count(axis=0, numeric_only=False, **kwargs)[source]
Return the count of non-null values in the Series.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the count.
- Return type:
Union[PandasApiTdsFrame,Series]
Notes
Equivalent to
series.aggregate("count"). Maps to SQLCOUNT(column).Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar integer.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].count().to_pandas()
Order Id 0 830
cume_dist_legend_ext
- Series.cume_dist_legend_ext(ascending=True)[source]
Compute the cumulative distribution of this column.
PyLegend extension — not present in pandas.
Maps to SQL
CUME_DIST() OVER (ORDER BY col)and PurecumulativeDistribution.- Parameters:
ascending (
bool) – Whether to order in ascending direction.- Returns:
A series containing cumulative distribution values (floats between 0 and 1).
- Return type:
Series
See also
rankCompute ranks.
ntile_legend_extAssign rows to numbered buckets.
Notes
Differences from pandas:
This method has no pandas equivalent.
CUME_DISTis exposed as a pylegend extension.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["CumeDist"] = frame["Order Id"].cume_dist_legend_ext() frame.head(5).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name CumeDist 0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier 0.001205 1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten 0.00241 2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes 0.003614 3 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock 0.004819 4 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices 0.006024
expanding
- Series.expanding(min_periods=1, axis=0, method=None, order_by=None, ascending=True)[source]
Create an expanding (cumulative) window on this column.
An expanding window includes all rows from the start of the frame up to the current row, enabling running totals, running averages, and similar cumulative calculations on a single column.
- Parameters:
min_periods (
int) – Minimum number of observations required to produce a value.axis (
Union[int,str]) – Only0/'index'is supported.method (
Optional[str]) – Not supported. Must beNone.order_by (
Union[str,Sequence[str],None]) – Column(s) to order by within the window.ascending (
Union[bool,Sequence[bool]]) – Sort direction(s) fororder_bycolumns.
- Returns:
A window series on which aggregates (
sum,mean, etc.) can be called.- Return type:
WindowSeries- Raises:
NotImplementedError – If
axisis not0, ormethodis notNone.
See also
rollingFixed-size sliding window on a column.
window_frame_legend_extCustom window specification.
Notes
Differences from pandas:
order_byandascendingare pylegend extensions not present in pandas.axis=1andmethodare not supported.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].expanding( order_by="Order Id" ).sum().to_pandas().head(5)
Order Id 0 10248 1 20497 2 30747 3 40998 4 51250
max
- Series.max(axis=0, skipna=True, numeric_only=False, **kwargs)[source]
Return the maximum of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported.numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the maximum value.
- Return type:
Union[PandasApiTdsFrame,Series]
Notes
Equivalent to
series.aggregate("max"). Works on string columns as well (lexicographic maximum).Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].max().to_pandas()
Order Id 0 11077
mean
- Series.mean(axis=0, skipna=True, numeric_only=False, **kwargs)[source]
Return the mean of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported.numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the mean.
- Return type:
Union[PandasApiTdsFrame,Series]
Notes
Equivalent to
series.aggregate("mean"). Maps to SQLAVG().Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].mean().to_pandas()
Order Id 0 10662.5
min
- Series.min(axis=0, skipna=True, numeric_only=False, **kwargs)[source]
Return the minimum of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported.numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the minimum value.
- Return type:
Union[PandasApiTdsFrame,Series]
Notes
Equivalent to
series.aggregate("min"). Works on string columns as well (lexicographic minimum).Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].min().to_pandas()
Order Id 0 10248
ntile_legend_ext
- Series.ntile_legend_ext(num_buckets, ascending=True)[source]
Assign rows to numbered buckets based on this column’s ordering.
PyLegend extension — not present in pandas.
Maps to SQL
NTILE(n) OVER (ORDER BY col)and Purentile.- Parameters:
num_buckets (
int) – Number of buckets to distribute rows into.ascending (
bool) – Whether to order in ascending direction.
- Returns:
A series containing bucket numbers (1-based).
- Return type:
Series
See also
rankCompute ranks.
cume_dist_legend_extCumulative distribution.
Notes
Differences from pandas:
This method has no pandas equivalent.
NTILEis exposed as a pylegend extension.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Quartile"] = frame["Order Id"].ntile_legend_ext(4) frame.head(5).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name Quartile 0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier 1 1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten 1 2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes 1 3 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock 1 4 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices 1
rank
- Series.rank(axis=0, method='min', numeric_only=False, na_option='bottom', ascending=True, pct=False)[source]
Compute the rank of values in this Series.
Return a new
Seriescontaining the rank of each value. The result can be assigned back to the parent frame as a new column, or executed directly as a standalone single-column query.- Parameters:
axis (
Union[int,str]) – Must be0or'index'.method (
str) –How to rank equal values:
'min': Lowest rank in the group of ties (SQLRANK()).'first': Ranks by order of appearance (SQLROW_NUMBER()).'dense': Like'min'but no gaps (SQLDENSE_RANK()).
numeric_only (
bool) – IfTrue, only rank numeric columns.na_option (
str) – Only'bottom'is supported.ascending (
bool) – Whether to rank in ascending order.pct (
bool) – IfTrue, compute percentage ranks (SQLPERCENT_RANK()). Returns aFloatSeries. Only supported withmethod='min'.
- Returns:
An
IntegerSeries(orFloatSerieswhenpct=True) containing the ranks.- Return type:
Series- Raises:
NotImplementedError – If called on a computed Series expression (e.g.
(frame['col'] + 5).rank()). Callrank()first, then apply arithmetic. Ifmethodis not'min','first', or'dense'. Ifna_optionis not'bottom'. Ifpct=Truewith a method other than'min'.
See also
PandasApiTdsFrame.rankRank all columns of a frame.
PandasApiGroupbyTdsFrame.rankRank within groups.
Notes
Differences from pandas:
The
'average'and'max'methods are not supported.na_optiononly supports'bottom'.pct=Trueis only supported withmethod='min'.The result is a
Series, not apandas.Series. It can be assigned to the frame or executed directly.Calling
rank()on a computed Series expression is not supported. Do the rank first, then apply arithmetic:frame['col'].rank() + 5.Only one window-function call is allowed per expression. To combine multiple, use separate assignments.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Execute a ranked Series directly as a single-column query frame["Order Id"].rank().to_pandas().head()
Order Id 0 1 1 2 2 3 3 4 4 5 # Assign a rank column to the frame frame["Order Rank"] = frame["Order Id"].rank() frame.head(5).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name Order Rank 0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier 1 1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten 2 2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes 3 3 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock 4 4 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices 5
rolling
- Series.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None, step=None, method=None, order_by=None, ascending=True)[source]
Create a fixed-size sliding window on this column.
A rolling window includes a fixed number of preceding rows for each row, enabling moving averages, moving sums, and similar calculations on a single column.
- Parameters:
window (
int) – Size of the moving window (number of rows).min_periods (
Optional[int]) – Minimum observations required. Defaults towindow.center (
bool) – Not supported. Must beFalse.win_type (
Optional[str]) – Not supported. Must beNone.on (
Optional[str]) – Not supported. Must beNone.axis (
Union[int,str]) – Only0/'index'is supported.closed (
Optional[str]) – Not supported. Must beNone.step (
Optional[int]) – Not supported. Must beNone.method (
Optional[str]) – Not supported. Must beNone.order_by (
Union[str,Sequence[str],None]) – Column(s) to order by within the window.ascending (
Union[bool,Sequence[bool]]) – Sort direction(s) fororder_bycolumns.
- Returns:
A window series on which aggregates (
sum,mean, etc.) can be called.- Return type:
WindowSeries- Raises:
NotImplementedError – If
center,win_type,on,closed,step, ormethodare set to non-default values, oraxisis not0.
See also
expandingExpanding (cumulative) window on a column.
window_frame_legend_extCustom window specification.
Notes
Differences from pandas:
order_byandascendingare pylegend extensions not present in pandas.center,win_type,on,closed,step, andmethodare not supported.axis=1is not supported.
Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].rolling( window=3, order_by="Order Id" ).mean().to_pandas().head(5)
Order Id 0 10248.0 1 10248.5 2 10249.0 3 10250.0 4 10251.0
std
- Series.std(axis=0, skipna=True, ddof=1, numeric_only=False, **kwargs)[source]
Return the standard deviation of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported.ddof (
int) – Degrees of freedom.1for sample standard deviation (STDDEV_SAMP),0for population standard deviation (STDDEV_POP).numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the standard deviation.
- Return type:
Union[PandasApiTdsFrame,Series]- Raises:
NotImplementedError – If
ddofis not0or1, or ifskipna,numeric_only, or**kwargsare set to unsupported values.
Notes
Equivalent to
series.aggregate("std")(ddof=1) orseries.aggregate("std_dev_population")(ddof=0). Maps to SQLSTDDEV_SAMP()orSTDDEV_POP().Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar. Onlyddof=0andddof=1are supported.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].std().to_pandas()
Order Id 0 239.744656
sum
- Series.sum(axis=0, skipna=True, numeric_only=False, min_count=0, **kwargs)[source]
Return the sum of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported (SQL aggregation ignores nulls by default).numeric_only (
bool) – Must beFalse.Trueis not supported.min_count (
int) – Must be0. Non-zero values are not supported.
- Returns:
A single-row frame with the sum.
- Return type:
Union[PandasApiTdsFrame,Series]
Notes
Equivalent to
series.aggregate("sum").Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar.skipna=False,numeric_only=True, andmin_count != 0are not supported.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].sum().to_pandas()
Order Id 0 8849875
var
- Series.var(axis=0, skipna=True, ddof=1, numeric_only=False, **kwargs)[source]
Return the variance of the Series values.
- Parameters:
axis (
Union[int,str]) – Must be0or'index'.skipna (
bool) – Must beTrue.Falseis not supported.ddof (
int) – Degrees of freedom.1for sample variance (VAR_SAMP),0for population variance (VAR_POP).numeric_only (
bool) – Must beFalse.Trueis not supported.
- Returns:
A single-row frame with the variance.
- Return type:
Union[PandasApiTdsFrame,Series]- Raises:
NotImplementedError – If
ddofis not0or1, or ifskipna,numeric_only, or**kwargsare set to unsupported values.
Notes
Equivalent to
series.aggregate("var")(ddof=1) orseries.aggregate("variance_population")(ddof=0). Maps to SQLVAR_SAMP()orVAR_POP().Differences from pandas: returns a single-row
PandasApiTdsFrameinstead of a scalar. Onlyddof=0andddof=1are supported.Examples
import pylegend frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["Order Id"].var().to_pandas()
Order Id 0 57477.5
window_frame_legend_ext
- Series.window_frame_legend_ext(frame_spec=<pylegend.core.language.pandas_api.pandas_api_frame_spec.RowsBetween object>, order_by=None, ascending=True)[source]
Create a custom window specification on this column.
PyLegend extension — not present in pandas.
The
frame_specargument controls theROWS BETWEENorRANGE BETWEENclause.- Parameters:
frame_spec (
Optional[FrameSpec]) – A window-frame specification created viarows_between()orrange_between().order_by (
Union[str,Sequence[str],None]) – Column(s) to order by within the window.ascending (
Union[bool,Sequence[bool]]) – Sort direction(s) fororder_bycolumns.
- Returns:
A window series on which aggregates can be called.
- Return type:
WindowSeries- Raises:
TypeError – If
frame_specis not aRowsBetweenorRangeBetween.
Notes
Differences from pandas:
This method has no pandas equivalent. It is a pylegend extension for fine-grained control over the SQL
ROWS BETWEEN/RANGE BETWEENclause.
Examples
import pylegend from pylegend.core.language.pandas_api.pandas_api_frame_spec import ( RowsBetween, ) frame = pylegend.samples.pandas_api.northwind_orders_frame()
spec = RowsBetween(-2, 0) frame["Order Id"].window_frame_legend_ext( spec, order_by="Order Id" ).sum().to_pandas().head(5)
Order Id 0 10248 1 20497 2 30747 3 30750 4 30753