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:1850–1881  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1848
1849@pytest.mark.numpy
1850def test_struct_from_tuples():
1851 ty = pa.struct([pa.field('a', pa.int32()),
1852 pa.field('b', pa.string()),
1853 pa.field('c', pa.bool_())])
1854
1855 data = [(5, 'foo', True),
1856 (6, 'bar', False)]
1857 expected = [{'a': 5, 'b': 'foo', 'c': True},
1858 {'a': 6, 'b': 'bar', 'c': False}]
1859 arr = pa.array(data, type=ty)
1860
1861 data_as_ndarray = np.empty(len(data), dtype=object)
1862 data_as_ndarray[:] = data
1863 arr2 = pa.array(data_as_ndarray, type=ty)
1864 assert arr.to_pylist() == expected
1865
1866 assert arr.equals(arr2)
1867
1868 # With omitted values
1869 data = [(5, 'foo', None),
1870 None,
1871 (6, None, False)]
1872 expected = [{'a': 5, 'b': 'foo', 'c': None},
1873 None,
1874 {'a': 6, 'b': None, 'c': False}]
1875 arr = pa.array(data, type=ty)
1876 assert arr.to_pylist() == expected
1877
1878 # Invalid tuple size
1879 for tup in [(5, 'foo'), (), ('5', 'foo', True, None)]:
1880 with pytest.raises(ValueError, match="(?i)tuple size"):
1881 pa.array([tup], type=ty)
1882
1883
1884def 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