Skip to content

Commit

Permalink
ARROW-17131: [Python] add public-facing field() to UnionType
Browse files Browse the repository at this point in the history
  • Loading branch information
anjakefala committed Aug 9, 2022
1 parent 3b8fee0 commit 231f2b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/pyarrow/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def test_struct_duplicate_field_names():
def test_union_type():
def check_fields(ty, fields):
assert ty.num_fields == len(fields)
assert [ty[i] for i in range(ty.num_fields)] == fields
assert [ty.field(i) for i in range(ty.num_fields)] == fields

fields = [pa.field('x', pa.list_(pa.int32())),
pa.field('y', pa.binary())]
Expand Down
19 changes: 19 additions & 0 deletions python/pyarrow/types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,28 @@ cdef class UnionType(DataType):
for i in range(len(self)):
yield self[i]

def field(self, i):
"""
Return a child field by its numeric index.
Parameters
----------
i : int
Returns
-------
pyarrow.Field
"""
if isinstance(i, int):
return DataType.field(self, i)
else:
raise TypeError('Expected integer')

def __getitem__(self, i):
"""
Return a child field by its index.
Alias of ``field``.
"""
return self.field(i)

Expand Down

0 comments on commit 231f2b3

Please sign in to comment.