メインコンテンツまでスキップ

アンラップudt

UDT データ型の列をその基になる型にアンラップします。

構文

Python
import pyspark.sql.functions as sf

sf.unwrap_udt(col=<col>)

パラメーター

パラメーター

Type

説明

col

pyspark.sql.Column または str

アンラップする UDT 列。

戻り値

pyspark.sql.Column: 基礎となる表現。

例 1 : ML 固有の UDT (VectorUDT) をアンラップします。

Python
from pyspark.sql import functions as sf
from pyspark.ml.linalg import Vectors

vec1 = Vectors.dense(1, 2, 3)
vec2 = Vectors.sparse(4, {1: 1.0, 3: 5.5})
df = spark.createDataFrame([(vec1,), (vec2,)], ["vec"])
df.select(sf.unwrap_udt("vec")).printSchema()
Output
root
|-- unwrap_udt(vec): struct (nullable = true)
| |-- type: byte (nullable = false)
| |-- size: integer (nullable = true)
| |-- indices: array (nullable = true)
| | |-- element: integer (containsNull = false)
| |-- values: array (nullable = true)
| | |-- element: double (containsNull = false)

例 2 : ML 固有の UDT (MatrixUDT) をアンラップします。

Python
from pyspark.sql import functions as sf
from pyspark.ml.linalg import Matrices

mat1 = Matrices.dense(2, 2, range(4))
mat2 = Matrices.sparse(2, 2, [0, 2, 3], [0, 1, 1], [2, 3, 4])
df = spark.createDataFrame([(mat1,), (mat2,)], ["mat"])
df.select(sf.unwrap_udt("mat")).printSchema()
Output
root
|-- unwrap_udt(mat): struct (nullable = true)
| |-- type: byte (nullable = false)
| |-- numRows: integer (nullable = false)
| |-- numCols: integer (nullable = false)
| |-- colPtrs: array (nullable = true)
| | |-- element: integer (containsNull = false)
| |-- rowIndices: array (nullable = true)
| | |-- element: integer (containsNull = false)
| |-- values: array (nullable = true)
| | |-- element: double (containsNull = false)
| |-- isTransposed: boolean (nullable = false)