Skip to content

BUG: melt should preserve Categorical id_vars #15853

Closed
@wavexx

Description

@wavexx

When using melt, I'd expect the type of the columns specified as id_vars to be preserved.
Categorical types seem to be lost in the process:

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())

shows:

None
   B variable  value
0  X        A      1
1  Y        A      2
<class 'pandas.core.frame.DataFrame'>
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)

Output of pd.show_versions()

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

pandas: 0.19.2
nose: 1.3.7
pip: None
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
statsmodels: 0.8.0.dev0+c906881
xarray: None
IPython: 5.1.0
sphinx: 1.4.9
patsy: 0.4.1+dev
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.7.3
bs4: None
html5lib: None
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.1.6
pymysql: None
psycopg2: None
jinja2: 2.9.5
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions