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

Function test_struct_from_arrays

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

Source from the content-addressed store, hash-verified

890
891
892def test_struct_from_arrays():
893 a = pa.array([4, 5, 6], type=pa.int64())
894 b = pa.array(["bar", None, ""])
895 c = pa.array([[1, 2], None, [3, None]])
896 expected_list = [
897 {'a': 4, 'b': 'bar', 'c': [1, 2]},
898 {'a': 5, 'b': None, 'c': None},
899 {'a': 6, 'b': '', 'c': [3, None]},
900 ]
901
902 # From field names
903 arr = pa.StructArray.from_arrays([a, b, c], ["a", "b", "c"])
904 assert arr.type == pa.struct(
905 [("a", a.type), ("b", b.type), ("c", c.type)])
906 assert arr.to_pylist() == expected_list
907
908 with pytest.raises(ValueError):
909 pa.StructArray.from_arrays([a, b, c], ["a", "b"])
910
911 arr = pa.StructArray.from_arrays([], [])
912 assert arr.type == pa.struct([])
913 assert arr.to_pylist() == []
914
915 # From fields
916 fa = pa.field("a", a.type, nullable=False)
917 fb = pa.field("b", b.type)
918 fc = pa.field("c", c.type)
919 arr = pa.StructArray.from_arrays([a, b, c], fields=[fa, fb, fc])
920 assert arr.type == pa.struct([fa, fb, fc])
921 assert not arr.type[0].nullable
922 assert arr.to_pylist() == expected_list
923
924 # From structtype
925 structtype = pa.struct([fa, fb, fc])
926 arr = pa.StructArray.from_arrays([a, b, c], type=structtype)
927 assert arr.type == pa.struct([fa, fb, fc])
928 assert not arr.type[0].nullable
929 assert arr.to_pylist() == expected_list
930
931 with pytest.raises(ValueError):
932 pa.StructArray.from_arrays([a, b, c], fields=[fa, fb])
933
934 arr = pa.StructArray.from_arrays([], fields=[])
935 assert arr.type == pa.struct([])
936 assert arr.to_pylist() == []
937
938 # Inconsistent fields
939 fa2 = pa.field("a", pa.int32())
940 with pytest.raises(ValueError, match="int64 vs int32"):
941 pa.StructArray.from_arrays([a, b, c], fields=[fa2, fb, fc])
942
943 arrays = [a, b, c]
944 fields = [fa, fb, fc]
945 # With mask
946 mask = pa.array([True, False, False])
947 arr = pa.StructArray.from_arrays(arrays, fields=fields, mask=mask)
948 assert arr.to_pylist() == [None] + expected_list[1:]
949

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