<h4 dir="auto">Python Code</h4>
<ul dir="auto">
<li><strong>Before pd.melt</strong></li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
                    'B': {0: 1, 1: 3, 2: 5},
                   'C': {0: 2, 1: 4, 2: 6}})
df['A'] = df.A.astype('category')
df"><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">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'A'</span>: {<span class="pl-c1">0</span>: <span class="pl-s">'a'</span>, <span class="pl-c1">1</span>: <span class="pl-s">'b'</span>, <span class="pl-c1">2</span>: <span class="pl-s">'c'</span>},
                    <span class="pl-s">'B'</span>: {<span class="pl-c1">0</span>: <span class="pl-c1">1</span>, <span class="pl-c1">1</span>: <span class="pl-c1">3</span>, <span class="pl-c1">2</span>: <span class="pl-c1">5</span>},
                   <span class="pl-s">'C'</span>: {<span class="pl-c1">0</span>: <span class="pl-c1">2</span>, <span class="pl-c1">1</span>: <span class="pl-c1">4</span>, <span class="pl-c1">2</span>: <span class="pl-c1">6</span>}})
<span class="pl-s1">df</span>[<span class="pl-s">'A'</span>] <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-v">A</span>.<span class="pl-en">astype</span>(<span class="pl-s">'category'</span>)
<span class="pl-s1">df</span></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/32193271/34827065-e751911e-f713-11e7-8306-d68fba62d12d.png"><img src="https://user-images.githubusercontent.com/32193271/34827065-e751911e-f713-11e7-8306-d68fba62d12d.png" alt="image" style="max-width: 100%;"></a></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="df.A.dtype"><pre class="notranslate"><span class="pl-s1">df</span>.<span class="pl-v">A</span>.<span class="pl-s1">dtype</span></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/32193271/34827604-a8323d06-f715-11e7-80d6-d7d07a6be5e5.png"><img src="https://user-images.githubusercontent.com/32193271/34827604-a8323d06-f715-11e7-80d6-d7d07a6be5e5.png" alt="image" style="max-width: 100%;"></a></p>
<ul dir="auto">
<li><strong>After pd.melt</strong></li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])"><pre class="notranslate"><span class="pl-s1">pd</span>.<span class="pl-en">melt</span>(<span class="pl-s1">df</span>, <span class="pl-s1">id_vars</span><span class="pl-c1">=</span>[<span class="pl-s">'A'</span>], <span class="pl-s1">value_vars</span><span class="pl-c1">=</span>[<span class="pl-s">'B'</span>, <span class="pl-s">'C'</span>])</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/32193271/34827660-e2112ac8-f715-11e7-97ab-e60d5880376d.png"><img src="https://user-images.githubusercontent.com/32193271/34827660-e2112ac8-f715-11e7-97ab-e60d5880376d.png" alt="image" style="max-width: 100%;"></a></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="df.A.dtype"><pre class="notranslate"><span class="pl-s1">df</span>.<span class="pl-v">A</span>.<span class="pl-s1">dtype</span></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/32193271/34827628-c240e42c-f715-11e7-95d0-443aac91ebf6.png"><img src="https://user-images.githubusercontent.com/32193271/34827628-c240e42c-f715-11e7-95d0-443aac91ebf6.png" alt="image" style="max-width: 100%;"></a></p>
<h4 dir="auto">Problem description</h4>
<p dir="auto">As the example above. column A is category dtype before pd.melt.<br>
However, the category dtype lost after pd.melt.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">Expected consistence before and after pd.melt</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.3.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.21.1<br>
pytest: 3.2.1<br>
pip: 9.0.1<br>
setuptools: 36.5.0.post20170921<br>
Cython: 0.26.1<br>
numpy: 1.13.3<br>
scipy: 0.19.1<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.1.0<br>
sphinx: 1.6.3<br>
patsy: 0.4.1<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.1.0<br>
openpyxl: 2.4.8<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.1.0<br>
bs4: 4.6.0<br>
html5lib: 0.999999999<br>
sqlalchemy: 1.1.13<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">When using <code class="notranslate">melt</code>, I'd expect the type of the columns specified as <code class="notranslate">id_vars</code> to be preserved.<br>
Categorical types seem to be lost in the process:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd
data = pd.DataFrame({'A': [1,2], 'B': pd.Categorical(['X', 'Y'])})
print(data)
print(data.info())
melted = pd.melt(data, ['B'], ['A'])
print(melted)
print(melted.info())"><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">data</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'A'</span>: [<span class="pl-c1">1</span>,<span class="pl-c1">2</span>], <span class="pl-s">'B'</span>: <span class="pl-s1">pd</span>.<span class="pl-v">Categorical</span>([<span class="pl-s">'X'</span>, <span class="pl-s">'Y'</span>])})
<span class="pl-en">print</span>(<span class="pl-s1">data</span>)
<span class="pl-en">print</span>(<span class="pl-s1">data</span>.<span class="pl-en">info</span>())
<span class="pl-s1">melted</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">melt</span>(<span class="pl-s1">data</span>, [<span class="pl-s">'B'</span>], [<span class="pl-s">'A'</span>])
<span class="pl-en">print</span>(<span class="pl-s1">melted</span>)
<span class="pl-en">print</span>(<span class="pl-s1">melted</span>.<span class="pl-en">info</span>())</pre></div>
<p dir="auto">shows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="None
   B variable  value
0  X        A      1
1  Y        A      2
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
B           2 non-null object
variable    2 non-null object
value       2 non-null int64
dtypes: int64(1), object(2)"><pre class="notranslate"><code class="notranslate">None
   B variable  value
0  X        A      1
1  Y        A      2
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
B           2 non-null object
variable    2 non-null object
value       2 non-null int64
dtypes: int64(1), object(2)
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-2-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
<p dir="auto">pandas: 0.19.2<br>
nose: 1.3.7<br>
pip: None<br>
setuptools: 33.1.1<br>
Cython: 0.25.2<br>
numpy: 1.12.0<br>
scipy: 0.18.1<br>
statsmodels: 0.8.0.dev0+c906881<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: 1.4.9<br>
patsy: 0.4.1+dev<br>
dateutil: 2.5.3<br>
pytz: 2016.7<br>
blosc: None<br>
bottleneck: None<br>
tables: 3.3.0<br>
numexpr: 2.6.1<br>
matplotlib: 2.0.0<br>
openpyxl: 2.3.0<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: 3.7.3<br>
bs4: None<br>
html5lib: None<br>
httplib2: 0.9.2<br>
apiclient: None<br>
sqlalchemy: 1.1.6<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.5<br>
boto: None<br>
pandas_datareader: None</p>
</details>