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 8, 2022
1 parent 3b8fee0 commit 7a3a19e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/pyarrow/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ 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
assert [ty.field(c) for c in 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 7a3a19e

Please sign in to comment.