Use the sequence() function to generate an array of numbers from start to stop with given step.
Required Parameters
- start
- Specifies the starting value of the sequence (inclusive).
- stop
- Specifies the ending value of the sequence (inclusive).
Optional Parameters
- step
- Specifies the step size for the sequence.
Default value: 1
- atype
- Specifies the desired array type of the output.The 'atype' must be compatible with INTEGER type value.
Default value: ARRAY_INTEGER('[100]')
Example setup
>>> from teradataml.dataframe.functions import sequence
Load the data to run the example.
>>> load_example_data("dataframe", "sales")
Create a DataFrame on 'sales' table.
>>> df = DataFrame("sales")
>>> df
Output
Feb Jan Mar Apr datetime accounts Yellow Inc 90.0 NaN NaN NaN 04/01/2017 Jones LLC 200.0 150.0 140.0 180.0 04/01/2017 Red Inc 200.0 150.0 140.0 NaN 04/01/2017 Alpha Co 210.0 200.0 215.0 250.0 04/01/2017 Blue Inc 90.0 50.0 95.0 101.0 04/01/2017 Orange Inc 210.0 NaN NaN 250.0 04/01/2017
Example 1: Create an array column 'arr_seq' with sequence from 1 to 5
>>> res = df.assign(arr_seq = sequence(1, 5)) >>> res
Output
Feb Jan Mar Apr datetime arr_seq accounts Jones LLC 200.0 150.0 140.0 180.0 17/01/04 (1,2,3,4,5) Alpha Co 210.0 200.0 215.0 250.0 17/01/04 (1,2,3,4,5) Blue Inc 90.0 50.0 95.0 101.0 17/01/04 (1,2,3,4,5) Red Inc 200.0 150.0 140.0 NaN 17/01/04 (1,2,3,4,5) Yellow Inc 90.0 NaN NaN NaN 17/01/04 (1,2,3,4,5) Orange Inc 210.0 NaN NaN 250.0 17/01/04 (1,2,3,4,5)
Example 2: Create a array column 'arr_seq' with step size of 2 from 1 to 10
>>> res = df.assign(arr_seq = sequence(-10, 5 , 2, atype=ARRAY_BIGINT('[100]')))
>>> res
Output
Feb Jan Mar Apr datetime arr_seq accounts Blue Inc 90.0 50.0 95.0 101.0 17/01/04 (-10,-8,-6,-4,-2,0,2,4) Alpha Co 210.0 200.0 215.0 250.0 17/01/04 (-10,-8,-6,-4,-2,0,2,4) Orange Inc 210.0 NaN NaN 250.0 17/01/04 (-10,-8,-6,-4,-2,0,2,4) Red Inc 200.0 150.0 140.0 NaN 17/01/04 (-10,-8,-6,-4,-2,0,2,4) Jones LLC 200.0 150.0 140.0 180.0 17/01/04 (-10,-8,-6,-4,-2,0,2,4) Yellow Inc 90.0 NaN NaN NaN 17/01/04 (-10,-8,-6,-4,-2,0,2,4)
>>> res.tdtypes
Output
accounts VARCHAR(length=20, charset='LATIN')
Feb FLOAT()
Jan BIGINT()
Mar BIGINT()
Apr BIGINT()
datetime DATE()
arr_seq ARRAY_BIGINT('[100]')
Example 3: Create a array column 'arr_seq' with sequence in columns 'start_col', 'stop_col' and 'step_col'
>>> tdf = df.assign(start_col = 4, stop_col = -6, step_col = -3) >>> res = tdf.assign(arr_seq = sequence(tdf.start_col, tdf.stop_col, tdf.step_col)) >>> res
Output
Feb Jan Mar Apr datetime start_col step_col stop_col arr_seq accounts Orange Inc 210.0 NaN NaN 250.0 17/01/04 4 -3 -6 (4,1,-2,-5) Blue Inc 90.0 50.0 95.0 101.0 17/01/04 4 -3 -6 (4,1,-2,-5) Jones LLC 200.0 150.0 140.0 180.0 17/01/04 4 -3 -6 (4,1,-2,-5) Alpha Co 210.0 200.0 215.0 250.0 17/01/04 4 -3 -6 (4,1,-2,-5) Yellow Inc 90.0 NaN NaN NaN 17/01/04 4 -3 -6 (4,1,-2,-5) Red Inc 200.0 150.0 140.0 NaN 17/01/04 4 -3 -6 (4,1,-2,-5)