MCPcopy Create free account
hub / github.com/apache/arrow / test_struct_from_arrays

Function test_struct_from_arrays

python/pyarrow/tests/test_array.py:777–853  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

775
776
777def test_struct_from_arrays():
778 a = pa.array([4, 5, 6], type=pa.int64())
779 b = pa.array(["bar", None, ""])
780 c = pa.array([[1, 2], None, [3, None]])
781 expected_list = [
782 {'a': 4, 'b': 'bar', 'c': [1, 2]},
783 {'a': 5, 'b': None, 'c': None},
784 {'a': 6, 'b': '', 'c': [3, None]},
785 ]
786
787 # From field names
788 arr = pa.StructArray.from_arrays([a, b, c], ["a", "b", "c"])
789 assert arr.type == pa.struct(
790 [("a", a.type), ("b", b.type), ("c", c.type)])
791 assert arr.to_pylist() == expected_list
792
793 with pytest.raises(ValueError):
794 pa.StructArray.from_arrays([a, b, c], ["a", "b"])
795
796 arr = pa.StructArray.from_arrays([], [])
797 assert arr.type == pa.struct([])
798 assert arr.to_pylist() == []
799
800 # From fields
801 fa = pa.field("a", a.type, nullable=False)
802 fb = pa.field("b", b.type)
803 fc = pa.field("c", c.type)
804 arr = pa.StructArray.from_arrays([a, b, c], fields=[fa, fb, fc])
805 assert arr.type == pa.struct([fa, fb, fc])
806 assert not arr.type[0].nullable
807 assert arr.to_pylist() == expected_list
808
809 # From structtype
810 structtype = pa.struct([fa, fb, fc])
811 arr = pa.StructArray.from_arrays([a, b, c], type=structtype)
812 assert arr.type == pa.struct([fa, fb, fc])
813 assert not arr.type[0].nullable
814 assert arr.to_pylist() == expected_list
815
816 with pytest.raises(ValueError):
817 pa.StructArray.from_arrays([a, b, c], fields=[fa, fb])
818
819 arr = pa.StructArray.from_arrays([], fields=[])
820 assert arr.type == pa.struct([])
821 assert arr.to_pylist() == []
822
823 # Inconsistent fields
824 fa2 = pa.field("a", pa.int32())
825 with pytest.raises(ValueError, match="int64 vs int32"):
826 pa.StructArray.from_arrays([a, b, c], fields=[fa2, fb, fc])
827
828 arrays = [a, b, c]
829 fields = [fa, fb, fc]
830 # With mask
831 mask = pa.array([True, False, False])
832 arr = pa.StructArray.from_arrays(arrays, fields=fields, mask=mask)
833 assert arr.to_pylist() == [None] + expected_list[1:]
834

Callers

nothing calls this directly

Calls 4

chunked_arrayMethod · 0.80
is_nullMethod · 0.80
arrayMethod · 0.45
fieldMethod · 0.45

Tested by

no test coverage detected