Skip to content

Commit fba641f

Browse files
handle multiindex columns
1 parent eae7716 commit fba641f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pandas/core/reshape/melt.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
def melt(frame, id_vars=None, value_vars=None, var_name=None,
2626
value_name='value', col_level=None):
2727
# TODO: what about the existing index?
28+
if isinstance(frame.columns, ABCMultiIndex):
29+
cols = [x for c in frame.columns for x in c]
30+
else:
31+
cols = list(frame.columns)
2832
if id_vars is not None:
2933
if not is_list_like(id_vars):
3034
id_vars = [id_vars]
@@ -35,7 +39,7 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
3539
else:
3640
# Check that `id_vars` are in frame
3741
id_vars = list(id_vars)
38-
missing = Index(id_vars).difference(frame.columns)
42+
missing = Index(np.ravel(id_vars)).difference(cols)
3943
if not missing.empty:
4044
raise KeyError("The following 'id_vars' are not present"
4145
" in the DataFrame: {missing}"
@@ -53,7 +57,7 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
5357
else:
5458
value_vars = list(value_vars)
5559
# Check that `value_vars` are in frame
56-
missing = Index(value_vars).difference(frame.columns)
60+
missing = Index(np.ravel(value_vars)).difference(cols)
5761
if not missing.empty:
5862
raise KeyError("The following 'value_vars' are not present in"
5963
" the DataFrame: {missing}"

0 commit comments

Comments
 (0)