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

Function test_list_from_arrays

python/pyarrow/tests/test_array.py:1134–1185  ·  view source on GitHub ↗
(list_array_type, list_type_factory)

Source from the content-addressed store, hash-verified

1132 [(pa.ListArray, pa.list_),
1133 (pa.LargeListArray, pa.large_list)])
1134def test_list_from_arrays(list_array_type, list_type_factory):
1135 offsets_arr = np.array([0, 2, 5, 8], dtype='i4')
1136 offsets = pa.array(offsets_arr, type='int32')
1137 pyvalues = [b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h']
1138 values = pa.array(pyvalues, type='binary')
1139
1140 result = list_array_type.from_arrays(offsets, values)
1141 expected = pa.array([pyvalues[:2], pyvalues[2:5], pyvalues[5:8]],
1142 type=list_type_factory(pa.binary()))
1143
1144 assert result.equals(expected)
1145
1146 # With specified type
1147 typ = list_type_factory(pa.field("name", pa.binary()))
1148 result = list_array_type.from_arrays(offsets, values, typ)
1149 assert result.type == typ
1150 assert result.type.value_field.name == "name"
1151
1152 # With nulls
1153 offsets = [0, None, 2, 6]
1154 values = [b'a', b'b', b'c', b'd', b'e', b'f']
1155
1156 result = list_array_type.from_arrays(offsets, values)
1157 expected = pa.array([values[:2], None, values[2:]],
1158 type=list_type_factory(pa.binary()))
1159
1160 assert result.equals(expected)
1161
1162 # Another edge case
1163 offsets2 = [0, 2, None, 6]
1164 result = list_array_type.from_arrays(offsets2, values)
1165 expected = pa.array([values[:2], values[2:], None],
1166 type=list_type_factory(pa.binary()))
1167 assert result.equals(expected)
1168
1169 # raise on invalid array
1170 offsets = [1, 3, 10]
1171 values = np.arange(5)
1172 with pytest.raises(ValueError):
1173 list_array_type.from_arrays(offsets, values)
1174
1175 # Non-monotonic offsets
1176 offsets = [0, 3, 2, 6]
1177 values = list(range(6))
1178 result = list_array_type.from_arrays(offsets, values)
1179 with pytest.raises(ValueError):
1180 result.validate(full=True)
1181
1182 # mismatching type
1183 typ = list_type_factory(pa.binary())
1184 with pytest.raises(TypeError):
1185 list_array_type.from_arrays(offsets, values, type=typ)
1186
1187
1188@pytest.mark.parametrize(('list_array_type', 'list_type_factory'), (

Callers

nothing calls this directly

Calls 5

listFunction · 0.85
equalsMethod · 0.80
arrayMethod · 0.45
fieldMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected