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

Function test_struct_duplicate_fields

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

Source from the content-addressed store, hash-verified

743
744
745def test_struct_duplicate_fields():
746 ty = pa.struct([
747 pa.field('x', pa.int16()),
748 pa.field('y', pa.float32()),
749 pa.field('x', pa.int64()),
750 ])
751 s = pa.scalar([('x', 1), ('y', 2.0), ('x', 3)], type=ty)
752
753 assert list(s) == list(s.keys()) == ['x', 'y', 'x']
754 assert len(s) == 3
755 assert s == s
756 assert list(s.items()) == [
757 ('x', pa.scalar(1, pa.int16())),
758 ('y', pa.scalar(2.0, pa.float32())),
759 ('x', pa.scalar(3, pa.int64()))
760 ]
761
762 assert 'x' in s
763 assert 'y' in s
764 assert 'z' not in s
765 assert 0 not in s
766
767 # getitem with field names fails for duplicate fields, works for others
768 with pytest.raises(KeyError):
769 s['x']
770
771 assert isinstance(s['y'], pa.FloatScalar)
772 assert s['y'].as_py() == 2.0
773
774 # getitem with integer index works for all fields
775 assert isinstance(s[0], pa.Int16Scalar)
776 assert s[0].as_py() == 1
777 assert isinstance(s[1], pa.FloatScalar)
778 assert s[1].as_py() == 2.0
779 assert isinstance(s[2], pa.Int64Scalar)
780 assert s[2].as_py() == 3
781
782 assert "pyarrow.StructScalar" in repr(s)
783
784 with pytest.raises(ValueError, match="duplicate field names"):
785 s.as_py()
786
787
788def test_map(pickle_module):

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected