<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="
# This is a DOCbug simply to document what pd.DataFrame(dtype) currently does.
# It is non-obvious to new users, gives non-obvious error messages, and also behaves differently to read_csv(dtype)

# a) Leaving dtype=None in constructor will infer a wider type than necessary
df_cols = {'year':np.int32, 'month':np.int8}
df = pd.DataFrame(columns=df_cols.keys(), dtype=None, index=range(10), data=-1)
&gt;&gt;&gt; df.dtypes
month    int64
year     int64

# b) The doc doesn't explicitly say a list/dict/Series/array-like is not allowed (and if you
# pass in one the error is not very friendly). Also behaves differently to read_csv(dtype)
df = pd.DataFrame(columns=df_cols.keys(), dtype=np.int32, index=range(10), data=-1)
# df.dtypes shows they're all np.int32
# Fix up dtypes after declaration
for col,coltype in df_cols.items():
    df[col] = df[col].astype(coltype) 
"><pre class="notranslate"><span class="pl-c"># This is a DOCbug simply to document what pd.DataFrame(dtype) currently does.</span>
<span class="pl-c"># It is non-obvious to new users, gives non-obvious error messages, and also behaves differently to read_csv(dtype)</span>

<span class="pl-c"># a) Leaving dtype=None in constructor will infer a wider type than necessary</span>
<span class="pl-s1">df_cols</span> <span class="pl-c1">=</span> {<span class="pl-s">'year'</span>:<span class="pl-s1">np</span>.<span class="pl-s1">int32</span>, <span class="pl-s">'month'</span>:<span class="pl-s1">np</span>.<span class="pl-s1">int8</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">columns</span><span class="pl-c1">=</span><span class="pl-s1">df_cols</span>.<span class="pl-en">keys</span>(), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-en">range</span>(<span class="pl-c1">10</span>), <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-s1">dtypes</span>
<span class="pl-s1">month</span>    <span class="pl-s1">int64</span>
<span class="pl-s1">year</span>     <span class="pl-s1">int64</span>

<span class="pl-c"># b) The doc doesn't explicitly say a list/dict/Series/array-like is not allowed (and if you</span>
<span class="pl-c"># pass in one the error is not very friendly). Also behaves differently to read_csv(dtype)</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">columns</span><span class="pl-c1">=</span><span class="pl-s1">df_cols</span>.<span class="pl-en">keys</span>(), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-s1">int32</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-en">range</span>(<span class="pl-c1">10</span>), <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>)
<span class="pl-c"># df.dtypes shows they're all np.int32</span>
<span class="pl-c"># Fix up dtypes after declaration</span>
<span class="pl-k">for</span> <span class="pl-s1">col</span>,<span class="pl-s1">coltype</span> <span class="pl-c1">in</span> <span class="pl-s1">df_cols</span>.<span class="pl-en">items</span>():
    <span class="pl-s1">df</span>[<span class="pl-s1">col</span>] <span class="pl-c1">=</span> <span class="pl-s1">df</span>[<span class="pl-s1">col</span>].<span class="pl-en">astype</span>(<span class="pl-s1">coltype</span>) </pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">The DataFrame() doc doesn't explicitly say a list/dict/Series/array-like is not allowed (and if you pass in one the error is not very friendly). Also behaves differently to read_csv(dtype).<br>
Leaving dtype=None in constructor will infer a wider type than necessary.<br>
So in general you either set dtype=widest_necessary_type, or dtype=None and then manually fix them up after declaration, by casting with <code class="notranslate">astype()</code></p>
<h4 dir="auto">Expected Output</h4>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
python: 2.7.10.final.0
python-bits: 64
machine: x86_64
processor: i386
byteorder: little
pandas: 0.19.1
numpy: 1.11.2
scipy: 0.18.1
</details>
<p dir="auto">I have a dataframe called delivery and when I print(delivery.columns) I get the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Index(['Complemento_endereço', 'cnpj', 'Data_fundação', 'Número',
   'Razão_social', 'CEP', 'situacao_cadastral', 'situacao_especial', 'Rua',
   'Nome_Fantasia', 'last_revenue_normalized', 'last_revenue_year',
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
   'CNAEs_secundários', 'Pessoas', 'percent'],
  dtype='object')"><pre class="notranslate"><code class="notranslate">Index(['Complemento_endereço', 'cnpj', 'Data_fundação', 'Número',
   'Razão_social', 'CEP', 'situacao_cadastral', 'situacao_especial', 'Rua',
   'Nome_Fantasia', 'last_revenue_normalized', 'last_revenue_year',
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
   'CNAEs_secundários', 'Pessoas', 'percent'],
  dtype='object')
</code></pre></div>
<p dir="auto">Well, we can clearly see that there is a column 'Rua'.</p>
<p dir="auto">Also, if I print(delivery.Rua) I get a proper result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="82671                         R JUDITE MELO DOS SANTOS
817797                                R DOS GUAJAJARAS
180081           AV MARCOS PENTEADO DE ULHOA RODRIGUES
149373                                 AL MARIA TEREZA
455511                               AV RANGEL PESTANA
..."><pre class="notranslate"><code class="notranslate">82671                         R JUDITE MELO DOS SANTOS
817797                                R DOS GUAJAJARAS
180081           AV MARCOS PENTEADO DE ULHOA RODRIGUES
149373                                 AL MARIA TEREZA
455511                               AV RANGEL PESTANA
...
</code></pre></div>
<p dir="auto">Even if I write "if 'Rua' in delivery.columns: print('here I am')" it does print the 'here I am'. So 'Rua' is in fact there.</p>
<p dir="auto">Well, in the immediate line after I have this code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço','Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]"><pre class="notranslate"><code class="notranslate">delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço','Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]
</code></pre></div>
<p dir="auto">And voilá, I get this weird error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
File &quot;/file.py&quot;, line 45, in &lt;module&gt;
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]
   'CNAEs_secundários', 'Pessoas', 'percent'],
File &quot;/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py&quot;, line 1991, in __getitem__
  dtype='object')
return self._getitem_array(key)
File &quot;/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py&quot;, line 2035, in _getitem_array
indexer = self.ix._convert_to_indexer(key, axis=1)
File &quot;/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py&quot;, line 1214, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: &quot;['Rua'] not in index&quot;"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
File "/file.py", line 45, in &lt;module&gt;
   'Telefone', 'email', 'Capital_Social', 'Cidade', 'Estado',
   'Razão_social', 'name_bairro', 'Natureza_Jurídica', 'CNAE', '#CNAE',
'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]
   'CNAEs_secundários', 'Pessoas', 'percent'],
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 1991, in __getitem__
  dtype='object')
return self._getitem_array(key)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2035, in _getitem_array
indexer = self.ix._convert_to_indexer(key, axis=1)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py", line 1214, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['Rua'] not in index"
</code></pre></div>
<p dir="auto">Can someone help? I tried stackoverflow but no one could help. I'm starting to think I'm crazy and 'Rua' is an illusion of my troubled mind.</p>
<p dir="auto"><strong>ADDITIONAL INFO</strong></p>
<p dir="auto">I'm using this code right before the error line:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="delivery=pd.DataFrame()

for i in selection.index:
    sample=groups.get_group(selection['#CNAE'].loc[i]).sample(selection['samples'].loc[i])
    delivery=pd.concat((delivery,sample)).sort_values('Capital_Social',ascending=False)


print(delivery.columns)
print(delivery.Rua)
print(delivery.set_index('cnpj').columns)

delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço',
                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]"><pre class="notranslate"><code class="notranslate">delivery=pd.DataFrame()

for i in selection.index:
    sample=groups.get_group(selection['#CNAE'].loc[i]).sample(selection['samples'].loc[i])
    delivery=pd.concat((delivery,sample)).sort_values('Capital_Social',ascending=False)


print(delivery.columns)
print(delivery.Rua)
print(delivery.set_index('cnpj').columns)

delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Rua','Número','Complemento_endereço',
                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica','Pessoas' ]]
</code></pre></div>
<p dir="auto"><strong>EDIT</strong></p>
<p dir="auto">New weird stuff:<br>
I gave up and deleted 'Rua' from that last piece of code, wishing that it would work. For my surprise, I had the same problem but now with the column 'Número'.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Número','Complemento_endereço',
                                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica' ]]

KeyError: &quot;['Número'] not in index&quot;"><pre class="notranslate"><code class="notranslate">delivery=delivery.set_index('cnpj')[['Razão_social','Nome_Fantasia','Data_fundação','CEP','Estado','Cidade','Bairro','Número','Complemento_endereço',
                                                 'Telefone','email','Capital_Social', 'CNAE', '#CNAE', 'Natureza_Jurídica' ]]

KeyError: "['Número'] not in index"
</code></pre></div>
<p dir="auto"><strong>EDIT 2</strong></p>
<p dir="auto">And then I gave up on 'Número' and took it out. Then the same problem happened with 'Complemento_endereço'. Then I deleted 'Complemento_endereço'. And it happend to 'Telefone' and so on.</p>
<p dir="auto">** EDIT 3 **</p>
<p dir="auto">If I do a pd.show_versions(), that's the output:</p>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.5.0.final.0<br>
python-bits: 64<br>
OS: Darwin<br>
OS-release: 16.5.0<br>
machine: x86_64<br>
processor: i386<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None</p>
<p dir="auto">pandas: 0.18.1<br>
nose: None<br>
pip: 8.1.2<br>
setuptools: 18.2<br>
Cython: None<br>
numpy: 1.11.0<br>
scipy: 0.17.1<br>
statsmodels: 0.6.1<br>
xarray: None<br>
IPython: None<br>
sphinx: None<br>
patsy: 0.4.1<br>
dateutil: 2.5.3<br>
pytz: 2016.4<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: 1.5.1<br>
openpyxl: None<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: 4.5.1<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.3<br>
pymysql: 0.7.11.None<br>
psycopg2: None<br>
jinja2: None<br>
boto: None<br>
pandas_datareader: None<br>
None</p>