<h4 dir="auto">Code Sample (copy-pastable, MCVE)</h4>
<p dir="auto">Consider the following code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import io
import pandas as pd

# Trial FWF file:
data = io.StringIO('x10011\nx10012\nx10013\nx10024\nx20025\nx20026\nx20037\nx20038\n')

# Read and cast:
df1 = pd.read_fwf(data, widths=[2,3,1], header=None, dtype={0: str, 1: str, 2: int})
# Then index:
df1.set_index(1, inplace=True)

# Read, cast and index at once:
data.seek(0)
df2 = pd.read_fwf(data, widths=[2,3,1], header=None, dtype={0: str, 1: str, 2: int}, index_col=1)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">io</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-c"># Trial FWF file:</span>
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">io</span>.<span class="pl-v">StringIO</span>(<span class="pl-s">'x10011<span class="pl-cce">\n</span>x10012<span class="pl-cce">\n</span>x10013<span class="pl-cce">\n</span>x10024<span class="pl-cce">\n</span>x20025<span class="pl-cce">\n</span>x20026<span class="pl-cce">\n</span>x20037<span class="pl-cce">\n</span>x20038<span class="pl-cce">\n</span>'</span>)

<span class="pl-c"># Read and cast:</span>
<span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_fwf</span>(<span class="pl-s1">data</span>, <span class="pl-s1">widths</span><span class="pl-c1">=</span>[<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">1</span>], <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-c1">0</span>: <span class="pl-s1">str</span>, <span class="pl-c1">1</span>: <span class="pl-s1">str</span>, <span class="pl-c1">2</span>: <span class="pl-s1">int</span>})
<span class="pl-c"># Then index:</span>
<span class="pl-s1">df1</span>.<span class="pl-en">set_index</span>(<span class="pl-c1">1</span>, <span class="pl-s1">inplace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-c"># Read, cast and index at once:</span>
<span class="pl-s1">data</span>.<span class="pl-en">seek</span>(<span class="pl-c1">0</span>)
<span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_fwf</span>(<span class="pl-s1">data</span>, <span class="pl-s1">widths</span><span class="pl-c1">=</span>[<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">1</span>], <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-c1">0</span>: <span class="pl-s1">str</span>, <span class="pl-c1">1</span>: <span class="pl-s1">str</span>, <span class="pl-c1">2</span>: <span class="pl-s1">int</span>}, <span class="pl-s1">index_col</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">As I understand the <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_fwf.html" rel="nofollow">documentation</a> about control switches:</p>
<blockquote>
<p dir="auto"><code class="notranslate">dtype</code> : Type name or dict of column -&gt; type, default None<br>
Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32}<br>
<strong>Use str or object together with suitable na_values settings to preserve and not interpret dtype.</strong><br>
If converters are specified, they will be applied INSTEAD of dtype conversion.</p>
</blockquote>
<blockquote>
<p dir="auto"><code class="notranslate">index_col</code> : int or sequence or False, default None<br>
Column to use as the row labels of the DataFrame.<br>
If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters<br>
at the end of each line, you might consider index_col=False to force pandas to <em>not</em><br>
use the first column as the index (row names)</p>
</blockquote>
<p dir="auto"><strong>Both output should be equal but it is not.</strong></p>
<p dir="auto">When indexing at once using <code class="notranslate">index_col</code> switch, column is inferred to be <code class="notranslate">int</code> and casted, making the switch <code class="notranslate">dtype</code> useless in this case.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df1.index
Index(['001', '001', '001', '002', '002', '002', '003', '003'], dtype='object', name=1)

&gt;&gt;&gt; df2.index
Int64Index([1, 1, 1, 2, 2, 2, 3, 3], dtype='int64', name=1)

&gt;&gt;&gt; df1.equals(df2)
False"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df1</span>.<span class="pl-s1">index</span>
<span class="pl-v">Index</span>([<span class="pl-s">'001'</span>, <span class="pl-s">'001'</span>, <span class="pl-s">'001'</span>, <span class="pl-s">'002'</span>, <span class="pl-s">'002'</span>, <span class="pl-s">'002'</span>, <span class="pl-s">'003'</span>, <span class="pl-s">'003'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>, <span class="pl-s1">name</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">df2</span>.<span class="pl-s1">index</span>
<span class="pl-v">Int64Index</span>([<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>, <span class="pl-c1">3</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'int64'</span>, <span class="pl-s1">name</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">df1</span>.<span class="pl-en">equals</span>(<span class="pl-s1">df2</span>)
<span class="pl-c1">False</span></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I think the expected output of:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="df2 = pd.read_fwf(data, widths=[2,3,1], header=None, dtype={0: str, 1: str, 2: int}, index_col=1)"><pre class="notranslate"><span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_fwf</span>(<span class="pl-s1">data</span>, <span class="pl-s1">widths</span><span class="pl-c1">=</span>[<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">1</span>], <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-c1">0</span>: <span class="pl-s1">str</span>, <span class="pl-c1">1</span>: <span class="pl-s1">str</span>, <span class="pl-c1">2</span>: <span class="pl-s1">int</span>}, <span class="pl-s1">index_col</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)</pre></div>
<p dir="auto">Should be equal to:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="df1 = pd.read_fwf(data, widths=[2,3,1], header=None, dtype={0: str, 1: str, 2: int})
df1.set_index(1, inplace=True)"><pre class="notranslate"><span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_fwf</span>(<span class="pl-s1">data</span>, <span class="pl-s1">widths</span><span class="pl-c1">=</span>[<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">1</span>], <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-c1">0</span>: <span class="pl-s1">str</span>, <span class="pl-c1">1</span>: <span class="pl-s1">str</span>, <span class="pl-c1">2</span>: <span class="pl-s1">int</span>})
<span class="pl-s1">df1</span>.<span class="pl-en">set_index</span>(<span class="pl-c1">1</span>, <span class="pl-s1">inplace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<p dir="auto">If not, it just makes no sense to be able to protect columns from casting using <code class="notranslate">dtype</code> switch.<br>
For this reason, I think it is a kind of slight bug or inconsistency.</p>
<p dir="auto">Anyway, as provided in MCVE above, there exists a solution to circonvolve the problem.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0
pytest: 3.5.1
pip: 10.0.1
setuptools: 39.1.0
Cython: 0.28.2
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.4
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None"><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0
pytest: 3.5.1
pip: 10.0.1
setuptools: 39.1.0
Cython: 0.28.2
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.4
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
</code></pre></div>
</details>
<p dir="auto">xref <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="119565693" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/11728" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/11728/hovercard" href="https://github.com/pandas-dev/pandas/issues/11728">#11728</a> for the multi-index case<br>
xref <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="181842124" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/14379" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/14379/hovercard" href="https://github.com/pandas-dev/pandas/issues/14379">#14379</a> for converters</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np

from pandas.compat import StringIO

data = &quot;&quot;&quot;Internets,Spaceships
01,a
02,b
03,c
04,d
05,e
06,f
&quot;&quot;&quot;

# No leading zeroes in the index because it interprets the column as numeric
print pd.read_csv(StringIO.StringIO(data), index_col=&quot;Internets&quot;)

# Expected to see trailing zeroes in the index still, got 1,2,3,4 instead. Index is int64.
print pd.read_csv(StringIO.StringIO(data), index_col=&quot;Internets&quot;, dtype={&quot;Internets&quot;: np.object})

# Trailing zeroes now, index is object.
print pd.read_csv(StringIO.StringIO(data), dtype={&quot;Internets&quot;: np.object}).set_index(&quot;Internets&quot;)
"><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-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-k">from</span> <span class="pl-s1">pandas</span>.<span class="pl-s1">compat</span> <span class="pl-k">import</span> <span class="pl-v">StringIO</span>

<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s">"""Internets,Spaceships</span>
<span class="pl-s">01,a</span>
<span class="pl-s">02,b</span>
<span class="pl-s">03,c</span>
<span class="pl-s">04,d</span>
<span class="pl-s">05,e</span>
<span class="pl-s">06,f</span>
<span class="pl-s">"""</span>

<span class="pl-c"># No leading zeroes in the index because it interprets the column as numeric</span>
<span class="pl-k">print</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-v">StringIO</span>.<span class="pl-v">StringIO</span>(<span class="pl-s1">data</span>), <span class="pl-s1">index_col</span><span class="pl-c1">=</span><span class="pl-s">"Internets"</span>)

<span class="pl-c"># Expected to see trailing zeroes in the index still, got 1,2,3,4 instead. Index is int64.</span>
<span class="pl-k">print</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-v">StringIO</span>.<span class="pl-v">StringIO</span>(<span class="pl-s1">data</span>), <span class="pl-s1">index_col</span><span class="pl-c1">=</span><span class="pl-s">"Internets"</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-s">"Internets"</span>: <span class="pl-s1">np</span>.<span class="pl-s1">object</span>})

<span class="pl-c"># Trailing zeroes now, index is object.</span>
<span class="pl-k">print</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-v">StringIO</span>.<span class="pl-v">StringIO</span>(<span class="pl-s1">data</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span>{<span class="pl-s">"Internets"</span>: <span class="pl-s1">np</span>.<span class="pl-s1">object</span>}).<span class="pl-en">set_index</span>(<span class="pl-s">"Internets"</span>)</pre></div>
<p dir="auto">Version:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [1]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Darwin
OS-release: 13.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.2
nose: 1.3.4
Cython: None
numpy: 1.9.1
scipy: 0.15.1
statsmodels: 0.6.1
IPython: 2.1.0
sphinx: None
patsy: 0.3.0
dateutil: 2.4.0
pytz: 2011c
bottleneck: None
tables: None
numexpr: 2.4
matplotlib: 1.3.1
openpyxl: 2.0.4
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: None
bs4: 4.3.2
html5lib: 0.999
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: 0.9.6
pymysql: None
psycopg2: None"><pre class="notranslate"><code class="notranslate">In [1]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Darwin
OS-release: 13.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.2
nose: 1.3.4
Cython: None
numpy: 1.9.1
scipy: 0.15.1
statsmodels: 0.6.1
IPython: 2.1.0
sphinx: None
patsy: 0.3.0
dateutil: 2.4.0
pytz: 2011c
bottleneck: None
tables: None
numexpr: 2.4
matplotlib: 1.3.1
openpyxl: 2.0.4
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: None
bs4: 4.3.2
html5lib: 0.999
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: 0.9.6
pymysql: None
psycopg2: None
</code></pre></div>