Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
asfimport opened this issue Oct 28, 2021 · 1 comment
Closed

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

asfimport opened this issue Oct 28, 2021 · 1 comment

Comments

@asfimport
Copy link
Collaborator

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.

jorisvandenbossche pushed a commit that referenced this issue Aug 9, 2024
…43481)

### Rationale for this change
Currently you cannot directly access the names of all the fields within a StructType, and it is not obvious how to access each field either. This change allows people to directly access the fields and their names directly. See #30058.

### What changes are included in this PR?
I added a .names and .fields attribute to StructType. 
.names returns a list of the names of the fields in the StructType, and .fields returns a list of the fields in the StructType.

### Are these changes tested?
Yes. I made two tests to check if the new .names and .fields attributes would operate as intended. The tests check if the .names attribute would return a list of the names of each field and if .fields returns a list of the fields.

### Are there any user-facing changes?
Yes, there are two new attributes users can now use.

* GitHub Issue: #30058

Authored-by: Abhinand-J <wabhinandj@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
@jorisvandenbossche
Copy link
Member

Issue resolved by pull request 43481
#43481

@jorisvandenbossche jorisvandenbossche added this to the 18.0.0 milestone Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants