Skip to content

Commit 8c17925

Browse files
authored
ARROW-17131: [Python] add StructType().field(): returns a field by name or index (#13652)
Authored-by: anjakefala <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent fa33ca4 commit 8c17925

2 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎python/pyarrow/tests/test_types.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,24 @@ def test_struct_type():
577577

578578
assert ty['b'] == ty[2]
579579

580+
assert ty['b'] == ty.field('b')
581+
582+
assert ty[2] == ty.field(2)
583+
580584
# Not found
581585
with pytest.raises(KeyError):
582586
ty['c']
583587

588+
with pytest.raises(KeyError):
589+
ty.field('c')
590+
584591
# Neither integer nor string
585592
with pytest.raises(TypeError):
586593
ty[None]
587594

595+
with pytest.raises(TypeError):
596+
ty.field(None)
597+
588598
for a, b in zip(ty, fields):
589599
a == b
590600

@@ -634,6 +644,7 @@ def test_union_type():
634644
def check_fields(ty, fields):
635645
assert ty.num_fields == len(fields)
636646
assert [ty[i] for i in range(ty.num_fields)] == fields
647+
assert [ty.field(i) for i in range(ty.num_fields)] == fields
637648

638649
fields = [pa.field('x', pa.list_(pa.int32())),
639650
pa.field('y', pa.binary())]

‎python/pyarrow/types.pxi‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,23 @@ cdef class StructType(DataType):
429429
Examples
430430
--------
431431
>>> import pyarrow as pa
432+
433+
Accessing fields using direct indexing:
434+
432435
>>> struct_type = pa.struct({'x': pa.int32(), 'y': pa.string()})
433436
>>> struct_type[0]
434437
pyarrow.Field<x: int32>
435438
>>> struct_type['y']
436439
pyarrow.Field<y: string>
437440
441+
Accessing fields using ``field()``:
442+
443+
>>> struct_type.field(1)
444+
pyarrow.Field<y: string>
445+
>>> struct_type.field('x')
446+
pyarrow.Field<x: int32>
447+
448+
# Creating a schema from the struct type's fields:
438449
>>> pa.schema(list(struct_type))
439450
x: int32
440451
y: string
@@ -494,6 +505,41 @@ cdef class StructType(DataType):
494505
"""
495506
return self.struct_type.GetFieldIndex(tobytes(name))
496507

508+
def field(self, i):
509+
"""
510+
Select a field by its column name or numeric index.
511+
512+
Parameters
513+
----------
514+
i : int or str
515+
516+
Returns
517+
-------
518+
pyarrow.Field
519+
520+
Examples
521+
--------
522+
523+
>>> import pyarrow as pa
524+
>>> struct_type = pa.struct({'x': pa.int32(), 'y': pa.string()})
525+
526+
Select the second field:
527+
528+
>>> struct_type.field(1)
529+
pyarrow.Field<y: string>
530+
531+
Select the field named 'x':
532+
533+
>>> struct_type.field('x')
534+
pyarrow.Field<x: int32>
535+
"""
536+
if isinstance(i, (bytes, str)):
537+
return self.field_by_name(i)
538+
elif isinstance(i, int):
539+
return DataType.field(self, i)
540+
else:
541+
raise TypeError('Expected integer or string index')
542+
497543
def get_all_field_indices(self, name):
498544
"""
499545
Return sorted list of indices for the fields with the given name.
@@ -525,13 +571,10 @@ cdef class StructType(DataType):
525571
def __getitem__(self, i):
526572
"""
527573
Return the struct field with the given index or name.
574+
575+
Alias of ``field``.
528576
"""
529-
if isinstance(i, (bytes, str)):
530-
return self.field_by_name(i)
531-
elif isinstance(i, int):
532-
return self.field(i)
533-
else:
534-
raise TypeError('Expected integer or string index')
577+
return self.field(i)
535578

536579
def __reduce__(self):
537580
return struct, (list(self),)
@@ -579,9 +622,28 @@ cdef class UnionType(DataType):
579622
for i in range(len(self)):
580623
yield self[i]
581624

625+
def field(self, i):
626+
"""
627+
Return a child field by its numeric index.
628+
629+
Parameters
630+
----------
631+
i : int
632+
633+
Returns
634+
-------
635+
pyarrow.Field
636+
"""
637+
if isinstance(i, int):
638+
return DataType.field(self, i)
639+
else:
640+
raise TypeError('Expected integer')
641+
582642
def __getitem__(self, i):
583643
"""
584644
Return a child field by its index.
645+
646+
Alias of ``field``.
585647
"""
586648
return self.field(i)
587649

0 commit comments

Comments
 (0)