You are viewing documentation about an older version (1.12.1). View latest version

snowflake.snowpark.functions.array_insert

snowflake.snowpark.functions.array_insert(array: Union[Column, str], pos: Union[Column, str], element: Union[Column, str]) Column[source]

Returns an ARRAY containing all elements from the source ARRAY as well as the new element.

Parameters:
  • array – Column containing the source ARRAY.

  • pos – Column containing a (zero-based) position in the source ARRAY. The new element is inserted at this position. The original element from this position (if any) and all subsequent elements (if any) are shifted by one position to the right in the resulting array (i.e. inserting at position 0 has the same effect as using array_prepend). A negative position is interpreted as an index from the back of the array (e.g. -1 results in insertion before the last element in the array).

  • element – Column containing the element to be inserted. The new element is located at position pos. The relative order of the other elements from the source array is preserved.

Example::
>>> from snowflake.snowpark import Row
>>> df = session.create_dataframe([Row([1, 2]), Row([1, 3])], schema=["a"])
>>> df.select(array_insert("a", lit(0), lit(10)).alias("result")).show()
------------
|"RESULT"  |
------------
|[         |
|  10,     |
|  1,      |
|  2       |
|]         |
|[         |
|  10,     |
|  1,      |
|  3       |
|]         |
------------
Copy