Skip to content

Commit 0266052

Browse files
committed
Add posexplode to python
1 parent 153e8eb commit 0266052

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

python/pyspark/sql/functions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,27 @@ def explode(col):
16371637
return Column(jc)
16381638

16391639

1640+
@since(2.1)
1641+
def posexplode(col):
1642+
"""Returns a new row for each element with position in the given array or map.
1643+
1644+
>>> from pyspark.sql import Row
1645+
>>> eDF = spark.createDataFrame([Row(a=1, intlist=[1,2,3], mapfield={"a": "b"})])
1646+
>>> eDF.select(posexplode(eDF.intlist)).collect()
1647+
[Row(pos=0, col=1), Row(pos=1, col=2), Row(pos=2, col=3)]
1648+
1649+
>>> eDF.select(posexplode(eDF.mapfield)).show()
1650+
+---+---+-----+
1651+
|pos|key|value|
1652+
+---+---+-----+
1653+
| 0| a| b|
1654+
+---+---+-----+
1655+
"""
1656+
sc = SparkContext._active_spark_context
1657+
jc = sc._jvm.functions.posexplode(_to_java_column(col))
1658+
return Column(jc)
1659+
1660+
16401661
@ignore_unicode_prefix
16411662
@since(1.6)
16421663
def get_json_object(col, path):

0 commit comments

Comments
 (0)