Skip to content

Commit 52c5048

Browse files
mroeschkephofl
authored andcommitted
DEPR: Enforce melt(value_name) behavior (pandas-dev#49462)
1 parent ca28bde commit 52c5048

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ Removal of prior version deprecations/changes
383383
- Enforced disallowing ``dict`` or ``set`` objects in ``suffixes`` in :func:`merge` (:issue:`34810`)
384384
- Enforced disallowing :func:`merge` to produce duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)
385385
- Enforced disallowing using :func:`merge` or :func:`join` on a different number of levels (:issue:`34862`)
386+
- Enforced disallowing ``value_name`` argument in :func:`DataFrame.melt` to match an element in the :class:`DataFrame` columns (:issue:`35003`)
386387
- Removed setting Categorical._codes directly (:issue:`41429`)
387388
- Removed setting Categorical.categories directly (:issue:`47834`)
388389
- Removed argument ``inplace`` from :meth:`Categorical.add_categories`, :meth:`Categorical.remove_categories`, :meth:`Categorical.set_categories`, :meth:`Categorical.rename_categories`, :meth:`Categorical.reorder_categories`, :meth:`Categorical.set_ordered`, :meth:`Categorical.as_ordered`, :meth:`Categorical.as_unordered` (:issue:`37981`, :issue:`41118`, :issue:`41133`, :issue:`47834`)

pandas/core/reshape/melt.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
TYPE_CHECKING,
66
Hashable,
77
)
8-
import warnings
98

109
import numpy as np
1110

1211
from pandas.util._decorators import (
1312
Appender,
1413
deprecate_kwarg,
1514
)
16-
from pandas.util._exceptions import find_stack_level
1715

1816
from pandas.core.dtypes.common import (
1917
is_extension_array_dtype,
@@ -56,13 +54,9 @@ def melt(
5654
cols = list(frame.columns)
5755

5856
if value_name in frame.columns:
59-
warnings.warn(
60-
"This dataframe has a column name that matches the 'value_name' column "
61-
"name of the resulting Dataframe. "
62-
"In the future this will raise an error, please set the 'value_name' "
63-
"parameter of DataFrame.melt to a unique name.",
64-
FutureWarning,
65-
stacklevel=find_stack_level(),
57+
raise ValueError(
58+
f"value_name ({value_name}) cannot match an element in "
59+
"the DataFrame columns."
6660
)
6761

6862
if id_vars is not None:

pandas/tests/reshape/test_melt.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -1073,19 +1075,16 @@ def test_col_substring_of_stubname(self):
10731075
result = wide_to_long(wide_df, stubnames="PA", i=["node_id", "A"], j="time")
10741076
tm.assert_frame_equal(result, expected)
10751077

1076-
def test_warn_of_column_name_value(self):
1077-
# GH34731
1078-
# raise a warning if the resultant value column name matches
1078+
def test_raise_of_column_name_value(self):
1079+
# GH34731, enforced in 2.0
1080+
# raise a ValueError if the resultant value column name matches
10791081
# a name in the dataframe already (default name is "value")
10801082
df = DataFrame({"col": list("ABC"), "value": range(10, 16, 2)})
1081-
expected = DataFrame(
1082-
[["A", "col", "A"], ["B", "col", "B"], ["C", "col", "C"]],
1083-
columns=["value", "variable", "value"],
1084-
)
10851083

1086-
with tm.assert_produces_warning(FutureWarning):
1087-
result = df.melt(id_vars="value")
1088-
tm.assert_frame_equal(result, expected)
1084+
with pytest.raises(
1085+
ValueError, match=re.escape("value_name (value) cannot match")
1086+
):
1087+
df.melt(id_vars="value", value_name="value")
10891088

10901089
@pytest.mark.parametrize("dtype", ["O", "string"])
10911090
def test_missing_stubname(self, dtype):

0 commit comments

Comments
 (0)