()
| 833 | |
| 834 | |
| 835 | def test_struct_from_arrays(): |
| 836 | a = pa.array([4, 5, 6], type=pa.int64()) |
| 837 | b = pa.array(["bar", None, ""]) |
| 838 | c = pa.array([[1, 2], None, [3, None]]) |
| 839 | expected_list = [ |
| 840 | {'a': 4, 'b': 'bar', 'c': [1, 2]}, |
| 841 | {'a': 5, 'b': None, 'c': None}, |
| 842 | {'a': 6, 'b': '', 'c': [3, None]}, |
| 843 | ] |
| 844 | |
| 845 | # From field names |
| 846 | arr = pa.StructArray.from_arrays([a, b, c], ["a", "b", "c"]) |
| 847 | assert arr.type == pa.struct( |
| 848 | [("a", a.type), ("b", b.type), ("c", c.type)]) |
| 849 | assert arr.to_pylist() == expected_list |
| 850 | |
| 851 | with pytest.raises(ValueError): |
| 852 | pa.StructArray.from_arrays([a, b, c], ["a", "b"]) |
| 853 | |
| 854 | arr = pa.StructArray.from_arrays([], []) |
| 855 | assert arr.type == pa.struct([]) |
| 856 | assert arr.to_pylist() == [] |
| 857 | |
| 858 | # From fields |
| 859 | fa = pa.field("a", a.type, nullable=False) |
| 860 | fb = pa.field("b", b.type) |
| 861 | fc = pa.field("c", c.type) |
| 862 | arr = pa.StructArray.from_arrays([a, b, c], fields=[fa, fb, fc]) |
| 863 | assert arr.type == pa.struct([fa, fb, fc]) |
| 864 | assert not arr.type[0].nullable |
| 865 | assert arr.to_pylist() == expected_list |
| 866 | |
| 867 | # From structtype |
| 868 | structtype = pa.struct([fa, fb, fc]) |
| 869 | arr = pa.StructArray.from_arrays([a, b, c], type=structtype) |
| 870 | assert arr.type == pa.struct([fa, fb, fc]) |
| 871 | assert not arr.type[0].nullable |
| 872 | assert arr.to_pylist() == expected_list |
| 873 | |
| 874 | with pytest.raises(ValueError): |
| 875 | pa.StructArray.from_arrays([a, b, c], fields=[fa, fb]) |
| 876 | |
| 877 | arr = pa.StructArray.from_arrays([], fields=[]) |
| 878 | assert arr.type == pa.struct([]) |
| 879 | assert arr.to_pylist() == [] |
| 880 | |
| 881 | # Inconsistent fields |
| 882 | fa2 = pa.field("a", pa.int32()) |
| 883 | with pytest.raises(ValueError, match="int64 vs int32"): |
| 884 | pa.StructArray.from_arrays([a, b, c], fields=[fa2, fb, fc]) |
| 885 | |
| 886 | arrays = [a, b, c] |
| 887 | fields = [fa, fb, fc] |
| 888 | # With mask |
| 889 | mask = pa.array([True, False, False]) |
| 890 | arr = pa.StructArray.from_arrays(arrays, fields=fields, mask=mask) |
| 891 | assert arr.to_pylist() == [None] + expected_list[1:] |
| 892 |
nothing calls this directly
no test coverage detected