astropy/astropy — pull request #1991 by taldcroft (2014-01-23 01:23:32+00:00)
Title: io.ascii: Passing `names` that already exist in a table

When reading a table with `io.ascii`, I can pass a `names` argument that tells me how the columns in the new table should be named. This overrules any column name that might be defined in the input column.

This feature has many uses, e.g. a table may be missing colum names or they might not be useful. This is all good:
```
In [1]: import astropy.io.ascii as ascii

In [3]: ascii.read(['a b','1 2'], names=['c','d'])
Out[3]:

array([(1, 2)],
dtype=[('c', ' in ()
----> 1 ascii.read(['a b','1 2'], names=['a','d'])

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/io/ascii/ui.pyc in read(table, guess, **kwargs)
152 guess = _GUESS
153 if guess:
--> 154 dat = _guess(table, new_kwargs)
155 else:
156 reader = get_reader(**new_kwargs)

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/io/ascii/ui.pyc in _guess(table, read_kwargs)
194
195 reader = get_reader(**guess_kwargs)
--> 196 dat = reader.read(table)
197
198 # When guessing require at least two columns

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/io/ascii/core.pyc in read(self, table)
867
868 _apply_include_exclude_names(table, self.names, self.include_names, self.exclude_names,
--> 869 self.strict_names)
870
871 return table

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/io/ascii/core.pyc in _apply_include_exclude_names(table, names, include_names, exclude_names, strict_names)
749 ' of table columns ({1})'.format(len(names), len(table.colnames)))
750 for name, colname in zip(names, table.colnames):
--> 751 table.rename_column(colname, name)
752
753 names = set(table.colnames)

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/table/table.pyc in rename_column(self, name, new_name)
1449 raise KeyError("Column {0} does not exist".format(name))
1450
-> 1451 self.columns[name].name = new_name
1452
1453 def add_row(self, vals=None, mask=None):

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/table/column.pyc in name(self, val)
268 if self.parent_table is not None:
269 table = self.parent_table
--> 270 table.columns._rename_column(self.name, val)
271 table._data.dtype.names = list(table.columns)
272 if table.masked:

/data/guenther/anaconda/lib/python2.7/site-packages/astropy-0.4.dev7010-py2.7-linux-x86_64.egg/astropy/table/table.pyc in _rename_column(self, name, new_name)
90 def _rename_column(self, name, new_name):
91 if new_name in self:
---> 92 raise KeyError("Column {0} already exists".format(new_name))
93
94 mapper = {name: new_name}

KeyError: u'Column a already exists'
```