Skip to content

Commit 76e39eb

Browse files
jschendelWillAyd
authored andcommitted
BUG: Fix melt with mixed int/str columns (#29792)
1 parent 6f1accd commit 76e39eb

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ Reshaping
604604
- Bug :meth:`Series.pct_change` where supplying an anchored frequency would throw a ValueError (:issue:`28664`)
605605
- Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`)
606606
- Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`)
607+
- Bug in :func:`melt` where supplying mixed strings and numeric values for ``id_vars`` or ``value_vars`` would incorrectly raise a ``ValueError`` (:issue:`29718`)
607608

608609

609610
Sparse

pandas/core/reshape/melt.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.core.dtypes.missing import notna
1212

1313
from pandas.core.arrays import Categorical
14+
import pandas.core.common as com
1415
from pandas.core.frame import DataFrame, _shared_docs
1516
from pandas.core.indexes.base import Index
1617
from pandas.core.reshape.concat import concat
@@ -47,7 +48,7 @@ def melt(
4748
else:
4849
# Check that `id_vars` are in frame
4950
id_vars = list(id_vars)
50-
missing = Index(np.ravel(id_vars)).difference(cols)
51+
missing = Index(com.flatten(id_vars)).difference(cols)
5152
if not missing.empty:
5253
raise KeyError(
5354
"The following 'id_vars' are not present"
@@ -69,7 +70,7 @@ def melt(
6970
else:
7071
value_vars = list(value_vars)
7172
# Check that `value_vars` are in frame
72-
missing = Index(np.ravel(value_vars)).difference(cols)
73+
missing = Index(com.flatten(value_vars)).difference(cols)
7374
if not missing.empty:
7475
raise KeyError(
7576
"The following 'value_vars' are not present in"

pandas/tests/reshape/test_melt.py

+16
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,22 @@ def test_melt_missing_columns_raises(self):
317317
):
318318
multi.melt(["A"], ["F"], col_level=0)
319319

320+
def test_melt_mixed_int_str_id_vars(self):
321+
# GH 29718
322+
df = DataFrame({0: ["foo"], "a": ["bar"], "b": [1], "d": [2]})
323+
result = melt(df, id_vars=[0, "a"], value_vars=["b", "d"])
324+
expected = DataFrame(
325+
{0: ["foo"] * 2, "a": ["bar"] * 2, "variable": list("bd"), "value": [1, 2]}
326+
)
327+
tm.assert_frame_equal(result, expected)
328+
329+
def test_melt_mixed_int_str_value_vars(self):
330+
# GH 29718
331+
df = DataFrame({0: ["foo"], "a": ["bar"]})
332+
result = melt(df, value_vars=[0, "a"])
333+
expected = DataFrame({"variable": [0, "a"], "value": ["foo", "bar"]})
334+
tm.assert_frame_equal(result, expected)
335+
320336

321337
class TestLreshape:
322338
def test_pairs(self):

0 commit comments

Comments
 (0)