(self)
| 1192 | } |
| 1193 | |
| 1194 | def test_auto_dict_encode(self): |
| 1195 | opts = ConvertOptions(auto_dict_encode=True) |
| 1196 | rows = "a,b\nab,1\ncdé,2\ncdé,3\nab,4".encode() |
| 1197 | table = self.read_bytes(rows, convert_options=opts) |
| 1198 | schema = pa.schema([('a', pa.dictionary(pa.int32(), pa.string())), |
| 1199 | ('b', pa.int64())]) |
| 1200 | expected = { |
| 1201 | 'a': ["ab", "cdé", "cdé", "ab"], |
| 1202 | 'b': [1, 2, 3, 4], |
| 1203 | } |
| 1204 | assert table.schema == schema |
| 1205 | assert table.to_pydict() == expected |
| 1206 | |
| 1207 | opts.auto_dict_max_cardinality = 2 |
| 1208 | table = self.read_bytes(rows, convert_options=opts) |
| 1209 | assert table.schema == schema |
| 1210 | assert table.to_pydict() == expected |
| 1211 | |
| 1212 | # Cardinality above max => plain-encoded |
| 1213 | opts.auto_dict_max_cardinality = 1 |
| 1214 | table = self.read_bytes(rows, convert_options=opts) |
| 1215 | assert table.schema == pa.schema([('a', pa.string()), |
| 1216 | ('b', pa.int64())]) |
| 1217 | assert table.to_pydict() == expected |
| 1218 | |
| 1219 | # With invalid UTF8, not checked |
| 1220 | opts.auto_dict_max_cardinality = 50 |
| 1221 | opts.check_utf8 = False |
| 1222 | rows = b"a,b\nab,1\ncd\xff,2\nab,3" |
| 1223 | table = self.read_bytes(rows, convert_options=opts, |
| 1224 | validate_full=False) |
| 1225 | assert table.schema == schema |
| 1226 | dict_values = table['a'].chunk(0).dictionary |
| 1227 | assert len(dict_values) == 2 |
| 1228 | assert dict_values[0].as_py() == "ab" |
| 1229 | assert dict_values[1].as_buffer() == b"cd\xff" |
| 1230 | |
| 1231 | # With invalid UTF8, checked |
| 1232 | opts.check_utf8 = True |
| 1233 | table = self.read_bytes(rows, convert_options=opts) |
| 1234 | schema = pa.schema([('a', pa.dictionary(pa.int32(), pa.binary())), |
| 1235 | ('b', pa.int64())]) |
| 1236 | expected = { |
| 1237 | 'a': [b"ab", b"cd\xff", b"ab"], |
| 1238 | 'b': [1, 2, 3], |
| 1239 | } |
| 1240 | assert table.schema == schema |
| 1241 | assert table.to_pydict() == expected |
| 1242 | |
| 1243 | def test_custom_nulls(self): |
| 1244 | # Infer nulls with custom values |
nothing calls this directly
no test coverage detected