Skip to content

[Python] Add StructType attribute to access all its fields #30058

@asfimport

Description

@asfimport

Currently when having a StructType, you can check the number of fields or get a specific field by index:

>>> struct_type = pa.struct([('a', pa.int64()), ('b', pa.float64())])
>>> struct_type.num_fields
2
>>> struct_type.get_field_index("a")
0
>>> struct_type[0]
pyarrow.Field<a: int64>

and so that means you can actually get all fields by iterating:

>>> list(struct_type)
[pyarrow.Field<a: int64>, pyarrow.Field<b: double>]

That's straightfoward code once you know this, but personally I don't find this very discoverable.

I think it would be nice to have some additional convenience attributes, such as .fields returning a list of all fields and .names returning a list of the field names (currently you need [f.name for f in struct_type] for this).

Reporter: Joris Van den Bossche / @jorisvandenbossche

Note: This issue was originally created as ARROW-14501. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions