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

Function test_struct_from_tuples

python/pyarrow/tests/test_convert_builtin.py:1873–1904  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1871
1872@pytest.mark.numpy
1873def test_struct_from_tuples():
1874 ty = pa.struct([pa.field('a', pa.int32()),
1875 pa.field('b', pa.string()),
1876 pa.field('c', pa.bool_())])
1877
1878 data = [(5, 'foo', True),
1879 (6, 'bar', False)]
1880 expected = [{'a': 5, 'b': 'foo', 'c': True},
1881 {'a': 6, 'b': 'bar', 'c': False}]
1882 arr = pa.array(data, type=ty)
1883
1884 data_as_ndarray = np.empty(len(data), dtype=object)
1885 data_as_ndarray[:] = data
1886 arr2 = pa.array(data_as_ndarray, type=ty)
1887 assert arr.to_pylist() == expected
1888
1889 assert arr.equals(arr2)
1890
1891 # With omitted values
1892 data = [(5, 'foo', None),
1893 None,
1894 (6, None, False)]
1895 expected = [{'a': 5, 'b': 'foo', 'c': None},
1896 None,
1897 {'a': 6, 'b': None, 'c': False}]
1898 arr = pa.array(data, type=ty)
1899 assert arr.to_pylist() == expected
1900
1901 # Invalid tuple size
1902 for tup in [(5, 'foo'), (), ('5', 'foo', True, None)]:
1903 with pytest.raises(ValueError, match="(?i)tuple size"):
1904 pa.array([tup], type=ty)
1905
1906
1907def test_struct_from_list_of_pairs():

Callers

nothing calls this directly

Calls 6

lenFunction · 0.85
equalsMethod · 0.80
fieldMethod · 0.45
stringMethod · 0.45
arrayMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected