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

Function test_table_from_pylist

python/pyarrow/tests/test_table.py:2514–2596  ·  view source on GitHub ↗
(cls)

Source from the content-addressed store, hash-verified

2512 ]
2513)
2514def test_table_from_pylist(cls):
2515 table = cls.from_pylist([])
2516 assert table.num_columns == 0
2517 assert table.num_rows == 0
2518 assert table.schema == pa.schema([])
2519 assert table.to_pylist() == []
2520
2521 schema = pa.schema([('strs', pa.utf8()), ('floats', pa.float64())])
2522
2523 # With lists as values
2524 data = [{'strs': '', 'floats': 4.5},
2525 {'strs': 'foo', 'floats': 5},
2526 {'strs': 'bar', 'floats': None}]
2527 table = cls.from_pylist(data)
2528 assert table.num_columns == 2
2529 assert table.num_rows == 3
2530 assert table.schema == schema
2531 assert table.to_pylist() == data
2532
2533 # With metadata and inferred schema
2534 metadata = {b'foo': b'bar'}
2535 schema = schema.with_metadata(metadata)
2536 table = cls.from_pylist(data, metadata=metadata)
2537 assert table.schema == schema
2538 assert table.schema.metadata == metadata
2539 assert table.to_pylist() == data
2540
2541 # With explicit schema
2542 table = cls.from_pylist(data, schema=schema)
2543 assert table.schema == schema
2544 assert table.schema.metadata == metadata
2545 assert table.to_pylist() == data
2546
2547 # Cannot pass both schema and metadata
2548 with pytest.raises(ValueError):
2549 cls.from_pylist(data, schema=schema, metadata=metadata)
2550
2551 # Non-convertible values given schema
2552 with pytest.raises(TypeError):
2553 cls.from_pylist([{'c0': 0}, {'c0': 1}, {'c0': 2}],
2554 schema=pa.schema([("c0", pa.string())]))
2555
2556 # Missing schema fields in the passed mapping translate to None
2557 schema = pa.schema([('a', pa.int64()),
2558 ('c', pa.int32()),
2559 ('d', pa.int16())
2560 ])
2561 table = cls.from_pylist(
2562 [{'a': 1, 'b': 3}, {'a': 2, 'b': 4}, {'a': 3, 'b': 5}],
2563 schema=schema
2564 )
2565 data = [{'a': 1, 'c': None, 'd': None},
2566 {'a': 2, 'c': None, 'd': None},
2567 {'a': 3, 'c': None, 'd': None}]
2568 assert table.schema == schema
2569 assert table.to_pylist() == data
2570
2571 # Passed wrong schema type

Callers

nothing calls this directly

Calls 2

schemaMethod · 0.45
stringMethod · 0.45

Tested by

no test coverage detected