Repository: astropy/astropy

Issue:
astropy.table.Table.add_columns crash if rename_duplicate=True when there are no duplicates
When adding columns to an astropy table with add_columns (or add_column), an error occurs if using rename_duplicate=True when there are in fact no duplicates. From the description of rename_duplicate in the documentation, it seems that this is a bug (I would hope so; it would be nice if the user doesn't have to know whether there are duplicates in order to set the rename_duplicate keyword accordingly).

Here is the error: 

> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/home/mikea/anaconda2/envs/astroconda/lib/python2.7/site-packages/astropy/table/table.py", line 1569, in add_column
>    self.add_columns([col], [index], name, rename_duplicate=rename_duplicate)
>  File "/home/mikea/anaconda2/envs/astroconda/lib/python2.7/site-packages/astropy/table/table.py", line 1704, in add_columns
>    existing_names.add(new_name)
>UnboundLocalError: local variable 'new_name' referenced before assignment

Here is some example code that produces the error:

    import numpy as np
    from astropy.table import Table, Column
    t = Table(data=[np.arange(6)], names='a')
    t.add_column(Column(data=np.random.random(6), name='c'), rename_duplicate=True)


Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 978a25f16c..1f7fa7f28b 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1074,6 +1074,9 @@ astropy.stats
 astropy.table
 ^^^^^^^^^^^^^
 
+- Fixed a bug in ``add_columns`` method where ``rename_duplicate=True`` would
+  cause an error if there were no duplicates. [#7540]
+
 astropy.tests
 ^^^^^^^^^^^^^
 
diff --git a/astropy/table/table.py b/astropy/table/table.py
index 8237cdf2f0..eedc36e446 100644
--- a/astropy/table/table.py
+++ b/astropy/table/table.py
@@ -1700,15 +1700,17 @@ class Table:
             for col in cols:
                 i = 1
                 orig_name = col.info.name
-                while col.info.name in existing_names:
+                if col.info.name in existing_names:
                     # If the column belongs to another table then copy it
                     # before renaming
-                    if col.info.parent_table is not None:
-                        col = col_copy(col)
-                    new_name = '{0}_{1}'.format(orig_name, i)
-                    col.info.name = new_name
-                    i += 1
-                existing_names.add(new_name)
+                    while col.info.name in existing_names:
+                        # Iterate until a unique name is found
+                        if col.info.parent_table is not None:
+                            col = col_copy(col)
+                        new_name = '{0}_{1}'.format(orig_name, i)
+                        col.info.name = new_name
+                        i += 1
+                    existing_names.add(new_name)
 
         self._init_from_cols(newcols)
 


Test patch:
diff --git a/astropy/table/tests/test_table.py b/astropy/table/tests/test_table.py
index 2ad6af96c2..0f370d26b5 100644
--- a/astropy/table/tests/test_table.py
+++ b/astropy/table/tests/test_table.py
@@ -573,6 +573,11 @@ class TestAddColumns(SetupData):
         # Check new column didn't change (since name conflict forced a copy)
         assert t['a_3'][0] == self.a[0]
 
+        # Check that rename_duplicate=True is ok if there are no duplicates
+        t.add_column(table_types.Column(name='q', data=[0, 1, 2]),
+                     rename_duplicate=True)
+        assert t.colnames == ['a', 'a_1', 'b', 'c', 'a_2', 'a_3', 'q']
+
     def test_add_duplicate_columns(self, table_types):
         self._setup(table_types)
         t = table_types.Table([self.a, self.b, self.c])