Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

jbradt opened this issue Feb 8, 2017 · 2 comments
Labels
API Design Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@jbradt
Copy link

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

@jreback
Copy link
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 jreback added API Design Bug Difficulty Novice Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 8, 2017
@jreback jreback added this to the 0.20.0 milestone Feb 8, 2017
@jbradt
Copy link
Author

jbradt commented Feb 8, 2017

Sure, I'll take a look at it.

jbradt pushed a commit to jbradt/pandas that referenced this issue Feb 8, 2017
AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
The value_vars argument of melt is now cast to list like the
id_vars argument.

closes pandas-dev#15348

Author: Joshua Bradt <[email protected]>
Author: Joshua Bradt <[email protected]>

Closes pandas-dev#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants