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

Method test_column_options

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

Source from the content-addressed store, hash-verified

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