| 1418 | ] |
| 1419 | ) |
| 1420 | def test_table_basics(cls): |
| 1421 | data = [ |
| 1422 | pa.array(range(5), type='int16'), |
| 1423 | pa.array([-10, -5, 0, None, 10], type='int32') |
| 1424 | ] |
| 1425 | table = cls.from_arrays(data, names=('a', 'b')) |
| 1426 | table.validate() |
| 1427 | |
| 1428 | assert not table.schema.metadata |
| 1429 | assert len(table) == 5 |
| 1430 | assert table.num_rows == 5 |
| 1431 | assert table.num_columns == len(data) |
| 1432 | assert table.shape == (5, 2) |
| 1433 | # (only the second array has a null bitmap) |
| 1434 | assert table.get_total_buffer_size() == (5 * 2) + (5 * 4 + 1) |
| 1435 | assert table.nbytes == (5 * 2) + (5 * 4 + 1) |
| 1436 | assert sys.getsizeof(table) >= object.__sizeof__( |
| 1437 | table) + table.get_total_buffer_size() |
| 1438 | |
| 1439 | pydict = table.to_pydict() |
| 1440 | assert pydict == OrderedDict([ |
| 1441 | ('a', [0, 1, 2, 3, 4]), |
| 1442 | ('b', [-10, -5, 0, None, 10]) |
| 1443 | ]) |
| 1444 | assert isinstance(pydict, dict) |
| 1445 | assert table == cls.from_pydict(pydict, schema=table.schema) |
| 1446 | |
| 1447 | with pytest.raises(IndexError): |
| 1448 | # bounds checking |
| 1449 | table[2] |
| 1450 | |
| 1451 | columns = [] |
| 1452 | for col in table.itercolumns(): |
| 1453 | |
| 1454 | if cls is pa.Table: |
| 1455 | assert type(col) is pa.ChunkedArray |
| 1456 | |
| 1457 | for chunk in col.iterchunks(): |
| 1458 | assert chunk is not None |
| 1459 | |
| 1460 | with pytest.raises(IndexError): |
| 1461 | col.chunk(-1) |
| 1462 | |
| 1463 | with pytest.raises(IndexError): |
| 1464 | col.chunk(col.num_chunks) |
| 1465 | |
| 1466 | else: |
| 1467 | assert issubclass(type(col), pa.Array) |
| 1468 | |
| 1469 | columns.append(col) |
| 1470 | |
| 1471 | assert table.columns == columns |
| 1472 | assert table == cls.from_arrays(columns, names=table.column_names) |
| 1473 | assert table != cls.from_arrays(columns[1:], names=table.column_names[1:]) |
| 1474 | assert table != columns |
| 1475 | |
| 1476 | # Schema passed explicitly |
| 1477 | schema = pa.schema([pa.field('c0', pa.int16(), |