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

Method test_auto_dict_encode

python/pyarrow/tests/test_csv.py:1179–1226  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1177 }
1178
1179 def test_auto_dict_encode(self):
1180 opts = ConvertOptions(auto_dict_encode=True)
1181 rows = "a,b\nab,1\ncdé,2\ncdé,3\nab,4".encode()
1182 table = self.read_bytes(rows, convert_options=opts)
1183 schema = pa.schema([('a', pa.dictionary(pa.int32(), pa.string())),
1184 ('b', pa.int64())])
1185 expected = {
1186 'a': ["ab", "cdé", "cdé", "ab"],
1187 'b': [1, 2, 3, 4],
1188 }
1189 assert table.schema == schema
1190 assert table.to_pydict() == expected
1191
1192 opts.auto_dict_max_cardinality = 2
1193 table = self.read_bytes(rows, convert_options=opts)
1194 assert table.schema == schema
1195 assert table.to_pydict() == expected
1196
1197 # Cardinality above max => plain-encoded
1198 opts.auto_dict_max_cardinality = 1
1199 table = self.read_bytes(rows, convert_options=opts)
1200 assert table.schema == pa.schema([('a', pa.string()),
1201 ('b', pa.int64())])
1202 assert table.to_pydict() == expected
1203
1204 # With invalid UTF8, not checked
1205 opts.auto_dict_max_cardinality = 50
1206 opts.check_utf8 = False
1207 rows = b"a,b\nab,1\ncd\xff,2\nab,3"
1208 table = self.read_bytes(rows, convert_options=opts,
1209 validate_full=False)
1210 assert table.schema == schema
1211 dict_values = table['a'].chunk(0).dictionary
1212 assert len(dict_values) == 2
1213 assert dict_values[0].as_py() == "ab"
1214 assert dict_values[1].as_buffer() == b"cd\xff"
1215
1216 # With invalid UTF8, checked
1217 opts.check_utf8 = True
1218 table = self.read_bytes(rows, convert_options=opts)
1219 schema = pa.schema([('a', pa.dictionary(pa.int32(), pa.binary())),
1220 ('b', pa.int64())])
1221 expected = {
1222 'a': [b"ab", b"cd\xff", b"ab"],
1223 'b': [1, 2, 3],
1224 }
1225 assert table.schema == schema
1226 assert table.to_pydict() == expected
1227
1228 def test_custom_nulls(self):
1229 # Infer nulls with custom values

Callers

nothing calls this directly

Calls 8

read_bytesMethod · 0.95
ConvertOptionsClass · 0.90
lenFunction · 0.85
as_pyMethod · 0.80
encodeMethod · 0.45
schemaMethod · 0.45
dictionaryMethod · 0.45
stringMethod · 0.45

Tested by

no test coverage detected