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

Function test_struct

python/pyarrow/tests/test_scalars.py:693–742  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

691
692
693def test_struct():
694 ty = pa.struct([
695 pa.field('x', pa.int16()),
696 pa.field('y', pa.float32())
697 ])
698
699 v = {'x': 2, 'y': 3.5}
700 s = pa.scalar(v, type=ty)
701 assert list(s) == list(s.keys()) == ['x', 'y']
702
703 assert list(s.values()) == [
704 pa.scalar(2, type=pa.int16()),
705 pa.scalar(3.5, type=pa.float32())
706 ]
707 assert list(s.items()) == [
708 ('x', pa.scalar(2, type=pa.int16())),
709 ('y', pa.scalar(3.5, type=pa.float32()))
710 ]
711 assert 'x' in s
712 assert 'y' in s
713 assert 'z' not in s
714 assert 0 not in s
715
716 assert s.as_py() == v
717 assert repr(s) != repr(v)
718 assert repr(s.as_py()) == repr(v)
719 assert len(s) == 2
720 assert isinstance(s['x'], pa.Int16Scalar)
721 assert isinstance(s['y'], pa.FloatScalar)
722 assert s['x'].as_py() == 2
723 assert s['y'].as_py() == 3.5
724 assert isinstance(s, Mapping)
725
726 with pytest.raises(KeyError):
727 s['nonexistent']
728
729 s = pa.scalar(None, type=ty)
730 assert list(s) == list(s.keys()) == ['x', 'y']
731 assert s.as_py() is None
732 assert 'x' in s
733 assert 'y' in s
734 assert isinstance(s['x'], pa.Int16Scalar)
735 assert isinstance(s['y'], pa.FloatScalar)
736 assert isinstance(s[0], pa.Int16Scalar)
737 assert isinstance(s[1], pa.FloatScalar)
738 assert s['x'].is_valid is False
739 assert s['y'].is_valid is False
740 assert s['x'].as_py() is None
741 assert s['y'].as_py() is None
742 assert isinstance(s, Mapping)
743
744
745def test_struct_duplicate_fields():

Callers

nothing calls this directly

Calls 7

listFunction · 0.85
lenFunction · 0.85
as_pyMethod · 0.80
fieldMethod · 0.45
scalarMethod · 0.45
keysMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected