Repository: pandas-dev/pandas

Issue:
BUG: read_csv: invalid index from header-only input file with multiindex.
``` Python
import pandas as pd
import cStringIO as stringio

df = pd.read_csv(stringio.StringIO("x,y,z"), index_col=["x", "y"])
print df.columns
print df.index.names
```

Output:

```
$ python x.py
Index([u'y'], dtype='object')
[u'x', u'y']
```

Expected column name is "z".
## INSTALLED VERSIONS

commit: 01995b2c2c759c66b208dc0bd8a35f5f23c39b9f
python: 2.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.18-400.1.1.el5
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB

pandas: 0.16.2-58-g01995b2
nose: 1.3.0
Cython: 0.22
numpy: 1.9.2
scipy: 0.9.0
statsmodels: None
IPython: 3.2.0
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.2
bottleneck: 0.6.0
tables: 3.2.0
numexpr: 2.4.3
matplotlib: 1.3.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.2.1
html5lib: 0.90
httplib2: $Rev$
apiclient: None
sqlalchemy: 0.9.4
pymysql: None
psycopg2: None



Discussion:
might be related to #10413 

pull-requests to fix welcome

Hi @jreback - I know where the problem lies and will create a PR for this.


Resolving patch:
diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py
--- a/pandas/io/parsers.py
+++ b/pandas/io/parsers.py
@@ -2223,13 +2223,14 @@ def _clean_index_names(columns, index_col):
 def _get_empty_meta(columns, index_col, index_names):
     columns = list(columns)
 
-    if index_col is not None:
-        index = MultiIndex.from_arrays([[]] * len(index_col),
-                                       names=index_names)
-        for n in index_col:
-            columns.pop(n)
-    else:
+    if index_col is None or index_col is False:
         index = Index([])
+    else:
+        index_col = list(index_col)
+        index = MultiIndex.from_arrays([[]] * len(index_col), names=index_names)
+        index_col.sort()
+        for i, n in enumerate(index_col):
+            columns.pop(n-i)
 
     return index, columns, {}
 


Test patch:
diff --git a/pandas/io/tests/test_parsers.py b/pandas/io/tests/test_parsers.py
--- a/pandas/io/tests/test_parsers.py
+++ b/pandas/io/tests/test_parsers.py
@@ -2301,6 +2301,81 @@ def test_empty_with_index(self):
         expected = DataFrame([], columns=['y'], index=Index([], name='x'))
         tm.assert_frame_equal(result, expected)
 
+    def test_emtpy_with_multiindex(self):
+        # GH 10467
+        data = 'x,y,z'
+        result = self.read_csv(StringIO(data), index_col=['x', 'y'])
+        expected = DataFrame([], columns=['z'],
+                             index=MultiIndex.from_arrays([[]] * 2, names=['x', 'y']))
+        tm.assert_frame_equal(result, expected)
+
+    def test_empty_with_reversed_multiindex(self):
+        data = 'x,y,z'
+        result = self.read_csv(StringIO(data), index_col=[1, 0])
+        expected = DataFrame([], columns=['z'],
+                             index=MultiIndex.from_arrays([[]] * 2, names=['y', 'x']))
+        tm.assert_frame_equal(result, expected)
+
+    def test_empty_index_col_scenarios(self):
+        data = 'x,y,z'
+
+        # None, no index
+        index_col, expected = None, DataFrame([], columns=list('xyz')),
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # False, no index
+        index_col, expected = False, DataFrame([], columns=list('xyz')),
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # int, first column
+        index_col, expected = 0, DataFrame([], columns=['y', 'z'], index=Index([], name='x'))
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # int, not first column
+        index_col, expected = 1, DataFrame([], columns=['x', 'z'], index=Index([], name='y'))
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # str, first column
+        index_col, expected = 'x', DataFrame([], columns=['y', 'z'], index=Index([], name='x'))
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # str, not the first column
+        index_col, expected = 'y', DataFrame([], columns=['x', 'z'], index=Index([], name='y'))
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # list of int
+        index_col, expected = [0, 1], DataFrame([], columns=['z'],
+                                                index=MultiIndex.from_arrays([[]] * 2, names=['x', 'y']))
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # list of str
+        index_col, expected = (
+            ['x', 'y'],
+            DataFrame([], columns=['z'], index=MultiIndex.from_arrays([[]] * 2, names=['x', 'y']))
+        )
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # list of int, reversed sequence
+        index_col, expected = (
+            [1, 0],
+            DataFrame([], columns=['z'], index=MultiIndex.from_arrays([[]] * 2, names=['y', 'x']))
+        )
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+        # list of str, reversed sequence
+        index_col, expected = (
+            ['y', 'x'],
+            DataFrame([], columns=['z'], index=MultiIndex.from_arrays([[]] * 2, names=['y', 'x']))
+        )
+        tm.assert_frame_equal(self.read_csv(StringIO(data), index_col=index_col), expected)
+
+    def test_empty_with_index_col_false(self):
+        # GH 10413
+        data = 'x,y'
+        result = self.read_csv(StringIO(data), index_col=False)
+        expected = DataFrame([], columns=['x', 'y'])
+        tm.assert_frame_equal(result, expected)
+
     def test_float_parser(self):
         # GH 9565
         data = '45e-1,4.5,45.,inf,-inf'