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

Method test_column_options

python/pyarrow/tests/test_csv.py:1793–1856  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1791 self.open_bytes(b"")
1792
1793 def test_column_options(self):
1794 # With column_names
1795 rows = b"1,2,3\n4,5,6"
1796 read_options = ReadOptions()
1797 read_options.column_names = ['d', 'e', 'f']
1798 reader = self.open_bytes(rows, read_options=read_options)
1799 expected_schema = pa.schema([('d', pa.int64()),
1800 ('e', pa.int64()),
1801 ('f', pa.int64())])
1802 self.check_reader(reader, expected_schema,
1803 [{'d': [1, 4],
1804 'e': [2, 5],
1805 'f': [3, 6]}])
1806
1807 # With include_columns
1808 convert_options = ConvertOptions()
1809 convert_options.include_columns = ['f', 'e']
1810 reader = self.open_bytes(rows, read_options=read_options,
1811 convert_options=convert_options)
1812 expected_schema = pa.schema([('f', pa.int64()),
1813 ('e', pa.int64())])
1814 self.check_reader(reader, expected_schema,
1815 [{'e': [2, 5],
1816 'f': [3, 6]}])
1817
1818 # With column_types
1819 convert_options.column_types = {'e': pa.string()}
1820 reader = self.open_bytes(rows, read_options=read_options,
1821 convert_options=convert_options)
1822 expected_schema = pa.schema([('f', pa.int64()),
1823 ('e', pa.string())])
1824 self.check_reader(reader, expected_schema,
1825 [{'e': ["2", "5"],
1826 'f': [3, 6]}])
1827
1828 # Missing columns in include_columns
1829 convert_options.include_columns = ['g', 'f', 'e']
1830 with pytest.raises(
1831 KeyError,
1832 match="Column 'g' in include_columns does not exist"):
1833 reader = self.open_bytes(rows, read_options=read_options,
1834 convert_options=convert_options)
1835
1836 convert_options.include_missing_columns = True
1837 reader = self.open_bytes(rows, read_options=read_options,
1838 convert_options=convert_options)
1839 expected_schema = pa.schema([('g', pa.null()),
1840 ('f', pa.int64()),
1841 ('e', pa.string())])
1842 self.check_reader(reader, expected_schema,
1843 [{'g': [None, None],
1844 'e': ["2", "5"],
1845 'f': [3, 6]}])
1846
1847 convert_options.column_types = {'e': pa.string(), 'g': pa.float64()}
1848 reader = self.open_bytes(rows, read_options=read_options,
1849 convert_options=convert_options)
1850 expected_schema = pa.schema([('g', pa.float64()),

Callers

nothing calls this directly

Calls 6

open_bytesMethod · 0.95
check_readerMethod · 0.95
ReadOptionsClass · 0.90
ConvertOptionsClass · 0.90
schemaMethod · 0.45
stringMethod · 0.45

Tested by

no test coverage detected