Skip to content

pd.melt does not allow a tuple for value_vars #15348

Closed
@jbradt

Description

@jbradt

Code Sample

>>> import pandas as pd

>>> df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6], 'D': [7, 8]})
>>> pd.melt(df, id_vars=('A', 'B'), value_vars=('C', 'D'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-ef146f73874a> in <module>()
----> 1 pd.melt(df, id_vars=('A', 'B'), value_vars=('C', 'D'))

/Users/josh/env/analysis/lib/python3.5/site-packages/pandas/core/reshape.py in melt(frame, id_vars, value_vars, var_name, value_name, col_level)
    766         if not isinstance(value_vars, (tuple, list, np.ndarray)):
    767             value_vars = [value_vars]
--> 768         frame = frame.ix[:, id_vars + value_vars]
    769     else:
    770         frame = frame.copy()

TypeError: can only concatenate list (not "tuple") to list

Problem description

The documentation says that id_vars and value_vars can be tuples, lists, or ndarrays, but it doesn't work if value_vars is a tuple. The function works as expected if value_vars is given as a list.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 16.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 26.1.1
Cython: 0.24.1
numpy: 1.12.0
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.2.3.1
numexpr: 2.6.1
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions