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

Function test_table_from_pandas_schema

python/pyarrow/tests/test_table.py:2539–2568  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2537
2538@pytest.mark.pandas
2539def test_table_from_pandas_schema():
2540 # passed schema is source of truth for the columns
2541 import pandas as pd
2542
2543 df = pd.DataFrame(OrderedDict([('strs', ['', 'foo', 'bar']),
2544 ('floats', [4.5, 5, None])]))
2545
2546 # with different but compatible schema
2547 schema = pa.schema([('strs', pa.utf8()), ('floats', pa.float32())])
2548 table = pa.Table.from_pandas(df, schema=schema)
2549 assert pa.types.is_float32(table.column('floats').type)
2550 assert table.schema.remove_metadata() == schema
2551
2552 # with different and incompatible schema
2553 schema = pa.schema([('strs', pa.utf8()), ('floats', pa.timestamp('s'))])
2554 with pytest.raises((NotImplementedError, TypeError)):
2555 pa.Table.from_pandas(df, schema=schema)
2556
2557 # schema has columns not present in data -> error
2558 schema = pa.schema([('strs', pa.utf8()), ('floats', pa.float64()),
2559 ('ints', pa.int64())])
2560 with pytest.raises(KeyError, match='ints'):
2561 pa.Table.from_pandas(df, schema=schema)
2562
2563 # data has columns not present in schema -> ignored
2564 schema = pa.schema([('strs', pa.utf8())])
2565 table = pa.Table.from_pandas(df, schema=schema)
2566 assert table.num_columns == 1
2567 assert table.schema.remove_metadata() == schema
2568 assert table.column_names == ['strs']
2569
2570
2571@pytest.mark.pandas

Callers

nothing calls this directly

Calls 2

schemaMethod · 0.45
columnMethod · 0.45

Tested by

no test coverage detected