pyspark.sql.functions.sequence

pyspark.sql.functions.sequence(start: ColumnOrName, stop: ColumnOrName, step: Optional[ColumnOrName] = None) → pyspark.sql.column.Column[source]

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

New in version 2.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
startColumn or str

starting value (inclusive)

stopColumn or str

last values (inclusive)

stepColumn or str, optional

value to add to current to get next element (default is 1)

Returns
Column

an array of sequence values

Examples

>>> df1 = spark.createDataFrame([(-2, 2)], ('C1', 'C2'))
>>> df1.select(sequence('C1', 'C2').alias('r')).collect()
[Row(r=[-2, -1, 0, 1, 2])]
>>> df2 = spark.createDataFrame([(4, -4, -2)], ('C1', 'C2', 'C3'))
>>> df2.select(sequence('C1', 'C2', 'C3').alias('r')).collect()
[Row(r=[4, 2, 0, -2, -4])]