nxcals.api.common.utils.array_utils.StructType.add
- StructType.add(field: str, data_type: str | DataType, nullable: bool = True, metadata: Dict[str, Any] | None = None) StructType
- StructType.add(field: StructField) StructType
Construct a
StructTypeby adding new elements to it, to define the schema. The method accepts either:A single parameter which is a
StructFieldobject.Between 2 and 4 parameters as (name, data_type, nullable (optional), metadata(optional). The data_type parameter may be either a String or a
DataTypeobject.
- Parameters:
field (str or
StructField) – Either the name of the field or aStructFieldobjectdata_type (
DataType, optional) – If present, the DataType of theStructFieldto createnullable (bool, optional) – Whether the field to add should be nullable (default True)
metadata (dict, optional) – Any additional metadata (default None)
- Return type:
Examples
>>> from pyspark.sql.types import IntegerType, StringType, StructField, StructType >>> struct1 = StructType().add("f1", StringType(), True).add("f2", StringType(), True, None) >>> struct2 = StructType([StructField("f1", StringType(), True), ... StructField("f2", StringType(), True, None)]) >>> struct1 == struct2 True >>> struct1 = StructType().add(StructField("f1", StringType(), True)) >>> struct2 = StructType([StructField("f1", StringType(), True)]) >>> struct1 == struct2 True >>> struct1 = StructType().add("f1", "string", True) >>> struct2 = StructType([StructField("f1", StringType(), True)]) >>> struct1 == struct2 True