diff --git a/python/pyarrow/tests/test_types.py b/python/pyarrow/tests/test_types.py index cabf69ed07af0..aaadad896b441 100644 --- a/python/pyarrow/tests/test_types.py +++ b/python/pyarrow/tests/test_types.py @@ -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())] diff --git a/python/pyarrow/types.pxi b/python/pyarrow/types.pxi index 1dae52f2fef81..1babbc41549c7 100644 --- a/python/pyarrow/types.pxi +++ b/python/pyarrow/types.pxi @@ -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)