| 825 | |
| 826 | |
| 827 | def test_recordbatch_select(): |
| 828 | a1 = pa.array([1, 2, 3, None, 5]) |
| 829 | a2 = pa.array(['a', 'b', 'c', 'd', 'e']) |
| 830 | a3 = pa.array([[1, 2], [3, 4], [5, 6], None, [9, 10]]) |
| 831 | batch = pa.record_batch([a1, a2, a3], ['f1', 'f2', 'f3']) |
| 832 | |
| 833 | # selecting with string names |
| 834 | result = batch.select(['f1']) |
| 835 | expected = pa.record_batch([a1], ['f1']) |
| 836 | assert result.equals(expected) |
| 837 | |
| 838 | result = batch.select(['f3', 'f2']) |
| 839 | expected = pa.record_batch([a3, a2], ['f3', 'f2']) |
| 840 | assert result.equals(expected) |
| 841 | |
| 842 | # selecting with integer indices |
| 843 | result = batch.select([0]) |
| 844 | expected = pa.record_batch([a1], ['f1']) |
| 845 | assert result.equals(expected) |
| 846 | |
| 847 | result = batch.select([2, 1]) |
| 848 | expected = pa.record_batch([a3, a2], ['f3', 'f2']) |
| 849 | assert result.equals(expected) |
| 850 | |
| 851 | # preserve metadata |
| 852 | batch2 = batch.replace_schema_metadata({"a": "test"}) |
| 853 | result = batch2.select(["f1", "f2"]) |
| 854 | assert b"a" in result.schema.metadata |
| 855 | |
| 856 | # selecting non-existing column raises |
| 857 | with pytest.raises(KeyError, match='Field "f5" does not exist'): |
| 858 | batch.select(['f5']) |
| 859 | |
| 860 | with pytest.raises(IndexError, match="index out of bounds"): |
| 861 | batch.select([5]) |
| 862 | |
| 863 | # duplicate selection gives duplicated names in resulting recordbatch |
| 864 | result = batch.select(['f2', 'f2']) |
| 865 | expected = pa.record_batch([a2, a2], ['f2', 'f2']) |
| 866 | assert result.equals(expected) |
| 867 | |
| 868 | # selection duplicated column raises |
| 869 | batch = pa.record_batch([a1, a2, a3], ['f1', 'f2', 'f1']) |
| 870 | with pytest.raises(KeyError, match='Field "f1" exists 2 times'): |
| 871 | batch.select(['f1']) |
| 872 | |
| 873 | result = batch.select(['f2']) |
| 874 | expected = pa.record_batch([a2], ['f2']) |
| 875 | assert result.equals(expected) |
| 876 | |
| 877 | |
| 878 | def test_recordbatch_from_struct_array_invalid(): |