polars.datatypes.Struct#
- class polars.datatypes.Struct(fields: Sequence[Field] | SchemaDict)[source]#
结构体复合类型。
- Parameters:
- fields
构成结构体的字段。可以是Field对象的序列,也可以是列名到数据类型的映射。
示例
使用字典进行初始化:
>>> dtype = pl.Struct({"a": pl.Int8, "b": pl.List(pl.String)}) >>> dtype Struct({'a': Int8, 'b': List(String)})
使用Field对象列表进行初始化:
>>> dtype = pl.Struct([pl.Field("a", pl.Int8), pl.Field("b", pl.List(pl.String))]) >>> dtype Struct({'a': Int8, 'b': List(String)})
初始化Series时,Polars可以从数据中推断出结构数据类型。
>>> s = pl.Series([{"a": 1, "b": ["x", "y"]}, {"a": 2, "b": ["z"]}]) >>> s shape: (2,) Series: '' [struct[2]] [ {1,["x", "y"]} {2,["z"]} ] >>> s.dtype Struct({'a': Int64, 'b': List(String)})
方法
__init__(fields)base_type()返回此数据类型的基本/根类型类。
from_python(py_type)返回与给定Python类型对应的Polars数据类型。
is_(other)检查此数据类型是否与另一个数据类型相同。
is_decimal()检查数据类型是否为小数类型。
is_float()检查数据类型是否为浮点类型。
is_integer()检查数据类型是否为整数类型。
is_nested()检查数据类型是否为嵌套类型。
is_numeric()检查数据类型是否为数值类型。
is_signed_integer()检查数据类型是否为有符号整数类型。
is_temporal()检查数据类型是否为时间类型。
is_unsigned_integer()检查数据类型是否为无符号整数类型。
to_python()返回与此Polars数据类型对应的Python类型。
to_schema()将Struct dtype返回为模式字典。
属性
fields