<p dir="auto">When you save MultiIndex DataFrame to csv and then loading it with no additional parameters pandas not able to read multiindices properly and reconstructed dataframe is incorrect, however all MultiIndices are stored. Creating MultiIndex DataFrame:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
&gt;&gt;&gt; tuples = list(zip(*arrays))
&gt;&gt;&gt; index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
&gt;&gt;&gt; df = pd.DataFrame(np.random.randn(6, 6), index=index[:6], columns=index[:6])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">arrays</span> <span class="pl-c1">=</span> [<span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-s">'bar'</span>, <span class="pl-s">'bar'</span>, <span class="pl-s">'baz'</span>, <span class="pl-s">'baz'</span>, <span class="pl-s">'foo'</span>, <span class="pl-s">'foo'</span>, <span class="pl-s">'qux'</span>, <span class="pl-s">'qux'</span>]),
<span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-s">'one'</span>, <span class="pl-s">'two'</span>, <span class="pl-s">'one'</span>, <span class="pl-s">'two'</span>, <span class="pl-s">'one'</span>, <span class="pl-s">'two'</span>, <span class="pl-s">'one'</span>, <span class="pl-s">'two'</span>])]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">tuples</span> <span class="pl-c1">=</span> <span class="pl-en">list</span>(<span class="pl-en">zip</span>(<span class="pl-c1">*</span><span class="pl-s1">arrays</span>))
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">index</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_tuples</span>(<span class="pl-s1">tuples</span>, <span class="pl-s1">names</span><span class="pl-c1">=</span>[<span class="pl-s">'first'</span>, <span class="pl-s">'second'</span>])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">6</span>, <span class="pl-c1">6</span>), <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">index</span>[:<span class="pl-c1">6</span>], <span class="pl-s1">columns</span><span class="pl-c1">=</span><span class="pl-s1">index</span>[:<span class="pl-c1">6</span>])</pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df
first              bar                 baz                 foo          
second             one       two       one       two       one       two
first second                                                            
bar   one     0.608879 -1.128122 -0.238610  1.089921  0.521766  1.116998
      two     1.161840  0.876528 -1.417781 -1.150305 -1.043776  0.458298
baz   one     1.761382  0.634688 -1.146492 -0.082379  0.414079 -0.520011
      two     1.079547  0.667917  0.122468 -1.112053  0.408232  0.703068
foo   one    -1.031256  1.439310  0.604550 -0.275087 -0.316610 -0.589309
      two     1.730061 -1.213560 -0.581617 -0.260879  0.069140  0.970397"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>
<span class="pl-s1">first</span>              <span class="pl-s1">bar</span>                 <span class="pl-s1">baz</span>                 <span class="pl-s1">foo</span>          
<span class="pl-s1">second</span>             <span class="pl-s1">one</span>       <span class="pl-s1">two</span>       <span class="pl-s1">one</span>       <span class="pl-s1">two</span>       <span class="pl-s1">one</span>       <span class="pl-s1">two</span>
<span class="pl-s1">first</span> <span class="pl-s1">second</span>                                                            
<span class="pl-s1">bar</span>   <span class="pl-s1">one</span>     <span class="pl-c1">0.608879</span> <span class="pl-c1">-</span><span class="pl-c1">1.128122</span> <span class="pl-c1">-</span><span class="pl-c1">0.238610</span>  <span class="pl-c1">1.089921</span>  <span class="pl-c1">0.521766</span>  <span class="pl-c1">1.116998</span>
      <span class="pl-s1">two</span>     <span class="pl-c1">1.161840</span>  <span class="pl-c1">0.876528</span> <span class="pl-c1">-</span><span class="pl-c1">1.417781</span> <span class="pl-c1">-</span><span class="pl-c1">1.150305</span> <span class="pl-c1">-</span><span class="pl-c1">1.043776</span>  <span class="pl-c1">0.458298</span>
<span class="pl-s1">baz</span>   <span class="pl-s1">one</span>     <span class="pl-c1">1.761382</span>  <span class="pl-c1">0.634688</span> <span class="pl-c1">-</span><span class="pl-c1">1.146492</span> <span class="pl-c1">-</span><span class="pl-c1">0.082379</span>  <span class="pl-c1">0.414079</span> <span class="pl-c1">-</span><span class="pl-c1">0.520011</span>
      <span class="pl-s1">two</span>     <span class="pl-c1">1.079547</span>  <span class="pl-c1">0.667917</span>  <span class="pl-c1">0.122468</span> <span class="pl-c1">-</span><span class="pl-c1">1.112053</span>  <span class="pl-c1">0.408232</span>  <span class="pl-c1">0.703068</span>
<span class="pl-s1">foo</span>   <span class="pl-s1">one</span>    <span class="pl-c1">-</span><span class="pl-c1">1.031256</span>  <span class="pl-c1">1.439310</span>  <span class="pl-c1">0.604550</span> <span class="pl-c1">-</span><span class="pl-c1">0.275087</span> <span class="pl-c1">-</span><span class="pl-c1">0.316610</span> <span class="pl-c1">-</span><span class="pl-c1">0.589309</span>
      <span class="pl-s1">two</span>     <span class="pl-c1">1.730061</span> <span class="pl-c1">-</span><span class="pl-c1">1.213560</span> <span class="pl-c1">-</span><span class="pl-c1">0.581617</span> <span class="pl-c1">-</span><span class="pl-c1">0.260879</span>  <span class="pl-c1">0.069140</span>  <span class="pl-c1">0.970397</span></pre></div>
<p dir="auto">Now saving to .csv</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df.to_csv('test.csv')"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">to_csv</span>(<span class="pl-s">'test.csv'</span>)</pre></div>
<p dir="auto">File content:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="first,,bar,bar,baz,baz,foo,foo
second,,one,two,one,two,one,two
first,second,,,,,,
bar,one,-0.218970051706,-0.37813933444,-0.7634843365,-0.704262374956,-0.839464555892,0.541422908532
bar,two,0.681850126209,-0.12138626844,0.361273042684,0.143076786043,0.89542332917,-1.4010526032
baz,one,-0.818985292735,-1.09454292831,0.162462638192,-1.03231385607,0.553385654032,0.585854960842
baz,two,-1.54054784208,1.00696690064,0.976815889071,-1.39618300884,0.283526891003,-0.942377368698
foo,one,1.08113073993,-0.29494275034,0.738820962724,-1.62865612926,1.10227198603,-0.628396328368
foo,two,0.135391399079,0.410272615409,0.59806563553,0.172448276698,0.11403085441,1.47679237362"><pre class="notranslate"><code class="notranslate">first,,bar,bar,baz,baz,foo,foo
second,,one,two,one,two,one,two
first,second,,,,,,
bar,one,-0.218970051706,-0.37813933444,-0.7634843365,-0.704262374956,-0.839464555892,0.541422908532
bar,two,0.681850126209,-0.12138626844,0.361273042684,0.143076786043,0.89542332917,-1.4010526032
baz,one,-0.818985292735,-1.09454292831,0.162462638192,-1.03231385607,0.553385654032,0.585854960842
baz,two,-1.54054784208,1.00696690064,0.976815889071,-1.39618300884,0.283526891003,-0.942377368698
foo,one,1.08113073993,-0.29494275034,0.738820962724,-1.62865612926,1.10227198603,-0.628396328368
foo,two,0.135391399079,0.410272615409,0.59806563553,0.172448276698,0.11403085441,1.47679237362
</code></pre></div>
<p dir="auto">Loading file:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df = pd.read_csv('test.csv')"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-s">'test.csv'</span>)</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    first Unnamed: 1             bar           bar.1              baz  \
0  second        NaN             one             two              one   
1   first     second             NaN             NaN              NaN   
2     bar        one  0.608879221531  -1.12812151143  -0.238610193059   
3     bar        two   1.16184022363  0.876527615412   -1.41778054545   
4     baz        one   1.76138211998  0.634687797737   -1.14649190565   
5     baz        two    1.0795474718  0.667917354209   0.122468371132   
6     foo        one  -1.03125632895   1.43930972259   0.604550496274   
7     foo        two   1.73006141389  -1.21356047236  -0.581617481203   

              baz.1              foo            foo.1  
0               two              one              two  
1               NaN              NaN              NaN  
2     1.08992073325   0.521766323704    1.11699823915  
3    -1.15030481312   -1.04377642582   0.458297920053  
4  -0.0823790312557   0.414078987249  -0.520010569472  
5    -1.11205302607    0.40823221543   0.703068488788  
6    -0.27508709062  -0.316610246209  -0.589308900542  
7   -0.260878563358  0.0691397545038     0.9703965012 "><pre class="notranslate"><code class="notranslate">    first Unnamed: 1             bar           bar.1              baz  \
0  second        NaN             one             two              one   
1   first     second             NaN             NaN              NaN   
2     bar        one  0.608879221531  -1.12812151143  -0.238610193059   
3     bar        two   1.16184022363  0.876527615412   -1.41778054545   
4     baz        one   1.76138211998  0.634687797737   -1.14649190565   
5     baz        two    1.0795474718  0.667917354209   0.122468371132   
6     foo        one  -1.03125632895   1.43930972259   0.604550496274   
7     foo        two   1.73006141389  -1.21356047236  -0.581617481203   

              baz.1              foo            foo.1  
0               two              one              two  
1               NaN              NaN              NaN  
2     1.08992073325   0.521766323704    1.11699823915  
3    -1.15030481312   -1.04377642582   0.458297920053  
4  -0.0823790312557   0.414078987249  -0.520010569472  
5    -1.11205302607    0.40823221543   0.703068488788  
6    -0.27508709062  -0.316610246209  -0.589308900542  
7   -0.260878563358  0.0691397545038     0.9703965012 
</code></pre></div>
<p dir="auto">Yes, I know that I need to specify columns and index parameters, but why I should do this? All information needed to restore original DataFrame with MultiIndex are already in the file. Is it possible to make it default to parse MultiIndices in csv, for example, using multiple separators in a row:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="first,second,,,,,,"><pre class="notranslate"><code class="notranslate">first,second,,,,,,
</code></pre></div>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
s1 = pd.Series(data = [1,2,3,4,5], index = [1,2,3,4,4])
s2 = pd.Series(data = [1,2,3,4,5], index = ['1', '2', '3', '4', '4'])

s1[0] # KeyError
s2[0] # KeyError

s1.iloc[:4][0] # KeyError
s1.iloc[:5][0] # KeyError

s2.iloc[:4][0] # Returns 1!
s2.iloc[:5][0] # TypeError &amp; KeyError"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-s1">s1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-s1">data</span> <span class="pl-c1">=</span> [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">5</span>], <span class="pl-s1">index</span> <span class="pl-c1">=</span> [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">4</span>])
<span class="pl-s1">s2</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">Series</span>(<span class="pl-s1">data</span> <span class="pl-c1">=</span> [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">5</span>], <span class="pl-s1">index</span> <span class="pl-c1">=</span> [<span class="pl-s">'1'</span>, <span class="pl-s">'2'</span>, <span class="pl-s">'3'</span>, <span class="pl-s">'4'</span>, <span class="pl-s">'4'</span>])

<span class="pl-s1">s1</span>[<span class="pl-c1">0</span>] <span class="pl-c"># KeyError</span>
<span class="pl-s1">s2</span>[<span class="pl-c1">0</span>] <span class="pl-c"># KeyError</span>

<span class="pl-s1">s1</span>.<span class="pl-s1">iloc</span>[:<span class="pl-c1">4</span>][<span class="pl-c1">0</span>] <span class="pl-c"># KeyError</span>
<span class="pl-s1">s1</span>.<span class="pl-s1">iloc</span>[:<span class="pl-c1">5</span>][<span class="pl-c1">0</span>] <span class="pl-c"># KeyError</span>

<span class="pl-s1">s2</span>.<span class="pl-s1">iloc</span>[:<span class="pl-c1">4</span>][<span class="pl-c1">0</span>] <span class="pl-c"># Returns 1!</span>
<span class="pl-s1">s2</span>.<span class="pl-s1">iloc</span>[:<span class="pl-c1">5</span>][<span class="pl-c1">0</span>] <span class="pl-c"># TypeError &amp; KeyError</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto"><code class="notranslate">s2[0]</code> throws a KeyError because the key, 0, is not in the index. This behavior is expected. <code class="notranslate">s2.iloc[:5][0]</code> also has this correct behavior. However, once the duplicate index has been removed, <code class="notranslate">s2.iloc[:4][0]</code>, this error is not thrown and instead the value at <strong>position</strong> 0 is returned.</p>
<p dir="auto">This error is present with string type indexes, but does not occur with integer indexes (see examples involving <code class="notranslate">s1</code> above). I have not tried other index types.</p>
<p dir="auto">Issue is present in a fresh conda environment with python version 3.6.2 and pandas version 0.21.1 (via conda-forge).</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto"><code class="notranslate">s2.iloc[:4][0]</code> should fail with TypeError and KeyError.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.2.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 2.6.32-696.13.2.el6.x86_64<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_US.UTF-8<br>
LOCALE: en_US.UTF-8</p>
<p dir="auto">pandas: 0.21.1<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 36.4.0<br>
Cython: None<br>
numpy: 1.13.1<br>
scipy: None<br>
pyarrow: None<br>
xarray: None<br>
IPython: None<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: None<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>