Repository: pandas-dev/pandas

Issue:
Wrong error from "reset_index()" when columns are MultiIndex and index name is incomplete column name
#### Code Sample, a copy-pastable example if possible

```python
In [2]: df = pd.DataFrame(index=range(2), columns=pd.MultiIndex.from_tuples([['A', 'a'], ['B', '']]))

In [3]: df.index.name = 'B'

In [4]: df.reset_index()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-6983677cc901> in <module>()
----> 1 df.reset_index()

/home/pietro/nobackup/repo/pandas/pandas/core/frame.py in reset_index(self, level, drop, inplace, col_level, col_fill)
   3056                     name = tuple(name_lst)
   3057             values = _maybe_casted_values(self.index)
-> 3058             new_obj.insert(0, name, values)
   3059 
   3060         new_obj.index = new_index

/home/pietro/nobackup/repo/pandas/pandas/core/frame.py in insert(self, loc, column, value, allow_duplicates)
   2517         value = self._sanitize_column(column, value, broadcast=False)
   2518         self._data.insert(loc, column, value,
-> 2519                           allow_duplicates=allow_duplicates)
   2520 
   2521     def assign(self, **kwargs):

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in insert(self, loc, item, value, allow_duplicates)
   3808         if not allow_duplicates and item in self.items:
   3809             # Should this be a different kind of error??
-> 3810             raise ValueError('cannot insert %s, already exists' % item)
   3811 
   3812         if not isinstance(loc, int):

TypeError: not all arguments converted during string formatting


```
#### Problem description

Clearly ``% item`` should be replaced with ``% (item,)``.

#### Expected Output

``cannot insert ('B', ''), already exists``

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.7.0-1-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: it_IT.utf8
LOCALE: it_IT.UTF-8

pandas: 0.20.0rc1+7.gf8b25c282
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
xarray: 0.9.1
IPython: 5.1.0.dev
sphinx: 1.4.9
patsy: 0.3.0-dev
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.6
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: 0.2.1

</details>



Discussion:
yep, must not be very well tested.
By the way:

``` python
df = pd.DataFrame(index=range(2), columns=pd.MultiIndex.from_tuples([['A', 'a'], ['B', 'b']]))
df.index.name = ('C', 'c')
df.reset_index()
```

... also fails, but I guess it's not supported (otherwise, I will open a new issue).
that's not supported. I suppose it could be but is pretty odd thing to do.

Resolving patch:
diff --git a/pandas/core/internals.py b/pandas/core/internals.py
--- a/pandas/core/internals.py
+++ b/pandas/core/internals.py
@@ -3807,7 +3807,7 @@ def insert(self, loc, item, value, allow_duplicates=False):
         """
         if not allow_duplicates and item in self.items:
             # Should this be a different kind of error??
-            raise ValueError('cannot insert %s, already exists' % item)
+            raise ValueError('cannot insert {}, already exists'.format(item))
 
         if not isinstance(loc, int):
             raise TypeError("loc must be int")


Test patch:
diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py
--- a/pandas/tests/test_multilevel.py
+++ b/pandas/tests/test_multilevel.py
@@ -2235,6 +2235,22 @@ def test_reset_index_period(self):
         }, columns=['month', 'feature', 'a'])
         tm.assert_frame_equal(df.reset_index(), expected)
 
+    def test_reset_index_multiindex_columns(self):
+        levels = [['A', ''], ['B', 'b']]
+        df = pd.DataFrame([[0, 2], [1, 3]],
+                          columns=pd.MultiIndex.from_tuples(levels))
+        expected = df.copy()
+        df.index.name = 'A'
+        result = df[['B']].reset_index()
+        tm.assert_frame_equal(result, expected)
+
+        # GH 16120
+        # already existing column
+        with tm.assertRaisesRegexp(ValueError,
+                                   ("cannot insert \('A', ''\), "
+                                    "already exists")):
+            df.reset_index()
+
     def test_set_index_period(self):
         # GH 6631
         df = DataFrame(np.random.random(6))