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

Closed
jbradt opened this Issue Feb 8, 2017 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

jbradt commented Feb 8, 2017

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

Contributor

jreback commented Feb 8, 2017

yep, these should use a little better validation here (and some more testing)

can you do a PR?

jreback added this to the 0.20.0 milestone Feb 8, 2017

Contributor

jbradt commented Feb 8, 2017

Sure, I'll take a look at it.

@jbradt jbradt added a commit to jbradt/pandas that referenced this issue Feb 8, 2017

@jbradt jbradt BUG: Fixed handling of non-list value_vars in melt
Closes #15348.
33728de

jreback closed this in 61deba5 Feb 10, 2017

@AnkurDedania AnkurDedania added a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017

@jbradt @AnkurDedania jbradt + AnkurDedania BUG: Fixed handling of non-list value_vars in melt
The value_vars argument of melt is now cast to list like the
id_vars argument.

closes #15348

Author: Joshua Bradt <bradt@nscl.msu.edu>
Author: Joshua Bradt <jbradt26@gmail.com>

Closes #15351 from jbradt/fix-melt and squashes the following commits:

a2f2510 [Joshua Bradt] Changed to tm.assertRaisesRegexp for Python 2 compat.
3038f64 [Joshua Bradt] Merge remote-tracking branch 'upstream/master' into fix-melt
e907135 [Joshua Bradt] Split test into two parts
20159c1 [Joshua Bradt] Changed exception classes to ValueError.
129d531 [Joshua Bradt] Moved binary operators to satisfy flake8
70d7256 [Joshua Bradt] Merge branch 'master' into fix-melt
455a310 [Joshua Bradt] Tested types when using MultiIndex to ensure they are lists.
7406222 [Joshua Bradt] Fixed formatting. Added comment with issue number to test.
d4c5da3 [Joshua Bradt] Improved type checking and tests. Added whatsnew note.
33728de [Joshua Bradt] BUG: Fixed handling of non-list value_vars in melt
021f594
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment