Skip to main content

sequence

Generate a sequence of integers from start to stop, incrementing by step. If step is not set, the function increments by 1 if start is less than or equal to stop, otherwise it decrements by 1.

Syntax

Python
from pyspark.sql import functions as sf

sf.sequence(start, stop, step=None)

Parameters

Parameter

Type

Description

start

pyspark.sql.Column or str

The starting value (inclusive) of the sequence.

stop

pyspark.sql.Column or str

The last value (inclusive) of the sequence.

step

pyspark.sql.Column or str, optional

The value to add to the current element to get the next element in the sequence. The default is 1 if start is less than or equal to stop, otherwise -1.

Returns

pyspark.sql.Column: A new column that contains an array of sequence values.

Examples

Example 1: Generating a sequence with default step

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(-2, 2)], ['start', 'stop'])
df.select(sf.sequence(df.start, df.stop)).show()
Output
+---------------------+
|sequence(start, stop)|
+---------------------+
| [-2, -1, 0, 1, 2]|
+---------------------+

Example 2: Generating a sequence with a custom step

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(4, -4, -2)], ['start', 'stop', 'step'])
df.select(sf.sequence(df.start, df.stop, df.step)).show()
Output
+---------------------------+
|sequence(start, stop, step)|
+---------------------------+
| [4, 2, 0, -2, -4]|
+---------------------------+

Example 3: Generating a sequence with a negative step

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(5, 1, -1)], ['start', 'stop', 'step'])
df.select(sf.sequence(df.start, df.stop, df.step)).show()
Output
+---------------------------+
|sequence(start, stop, step)|
+---------------------------+
| [5, 4, 3, 2, 1]|
+---------------------------+