maxframe.dataframe.Series.struct.field#
- Series.struct.field(name_or_index)#
将结构体的子字段提取为一个 Series。
- 参数:
name_or_index (str | bytes | int | expression | list) -- 要提取的子字段的名称或索引。对于类列表输入,这将索引到嵌套结构体中。
- 返回:
与所选子字段对应的数据。
- 返回类型:
参见
Series.struct.explode将所有子字段作为 DataFrame 返回。
备注
生成的 Series 的名称将根据以下规则设置:
对于字符串、字节或整数 `name_or_index`(或这些类型的列表,用于嵌套选择),Series 的名称将设置为所选字段的名称。
对于
pyarrow.compute.Expression,将设置为表达式的字符串形式。对于类列表的 name_or_index,名称将设置为最终所选字段的名称。
示例
>>> import maxframe.dataframe as md >>> import pandas as pd >>> import pyarrow as pa >>> s = md.Series( ... [ ... {"version": 1, "project": "pandas"}, ... {"version": 2, "project": "pandas"}, ... {"version": 1, "project": "numpy"}, ... ], ... dtype=pd.ArrowDtype(pa.struct( ... [("version", pa.int64()), ("project", pa.string())] ... )) ... )
按字段名称提取。
>>> s.struct.field("project").execute() 0 pandas 1 pandas 2 numpy Name: project, dtype: string[pyarrow]
按字段索引提取。
>>> s.struct.field(0).execute() 0 1 1 2 2 1 Name: version, dtype: int64[pyarrow]
对于嵌套结构体类型,可以传递一个值列表以索引多个层级:
>>> version_type = pa.struct([ ... ("major", pa.int64()), ... ("minor", pa.int64()), ... ]) >>> s = md.Series( ... [ ... {"version": {"major": 1, "minor": 5}, "project": "pandas"}, ... {"version": {"major": 2, "minor": 1}, "project": "pandas"}, ... {"version": {"major": 1, "minor": 26}, "project": "numpy"}, ... ], ... dtype=pd.ArrowDtype(pa.struct( ... [("version", version_type), ("project", pa.string())] ... )) ... ) >>> s.struct.field(["version", "minor"]).execute() 0 5 1 1 2 26 Name: minor, dtype: int64[pyarrow] >>> s.struct.field([0, 0]).execute() 0 1 1 2 2 1 Name: major, dtype: int64[pyarrow]