From d243026b9a7374985044d0802d312f46cf58244d Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:35:15 +1000 Subject: [PATCH 01/23] warn if value column name already exists --- pandas/core/reshape/melt.py | 6 ++++++ pandas/tests/reshape/test_melt.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 845f6b67693f4..e9512f2678844 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, List, cast import numpy as np +import warnings from pandas.util._decorators import Appender, deprecate_kwarg @@ -40,6 +41,11 @@ def melt( else: cols = list(frame.columns) + if value_name in frame.columns: + warnings.warn('The value column name "%s" conflicts with an existing' + ' column in the dataframe.' % (value_name), + DeprecationWarning) + if id_vars is not None: if not is_list_like(id_vars): id_vars = [id_vars] diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 000a6354277ab..00d9e58c9be73 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm +import warnings class TestMelt: @@ -1014,3 +1015,18 @@ def test_col_substring_of_stubname(self): ) result = pd.wide_to_long(wide_df, stubnames="PA", i=["node_id", "A"], j="time") tm.assert_frame_equal(result, expected) + + def test_warn_of_column_name_value(self): + # GH34731 + # raise a warning if the resultant value column name matches + # a name in the dataframe already (default name is "value") + df = pd.DataFrame({'col':list('ABC'), + 'value':range(10,16,2)}) + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + dfm = df.melt(id_vars='value') + assert len(w) == 1 + assert issubclass(w[-1].category, DeprecationWarning) + assert "conflicts" in str(w[-1].message) + From 968fc5b8b1c79350f43dfbd2ffcf39592ea07375 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:59:49 +1000 Subject: [PATCH 02/23] enblackenated my patch --- pandas/core/reshape/melt.py | 8 +++++--- pandas/tests/reshape/test_melt.py | 8 +++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index e9512f2678844..4b31b0bc39f19 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -42,9 +42,11 @@ def melt( cols = list(frame.columns) if value_name in frame.columns: - warnings.warn('The value column name "%s" conflicts with an existing' - ' column in the dataframe.' % (value_name), - DeprecationWarning) + warnings.warn( + 'The value column name "%s" conflicts with an existing' + " column in the dataframe." % (value_name), + DeprecationWarning, + ) if id_vars is not None: if not is_list_like(id_vars): diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 00d9e58c9be73..240f6bdd79e2e 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1020,13 +1020,11 @@ def test_warn_of_column_name_value(self): # GH34731 # raise a warning if the resultant value column name matches # a name in the dataframe already (default name is "value") - df = pd.DataFrame({'col':list('ABC'), - 'value':range(10,16,2)}) + df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars='value') + dfm = df.melt(id_vars="value") assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) - assert "conflicts" in str(w[-1].message) - + assert "conflicts" in str(w[-1].message) From 12bdaf7ac86476325b74dda4119d25f22aa1d1b9 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:19:30 +1000 Subject: [PATCH 03/23] added entry in whatsnew file --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 60aa1759958f6..558878a0ca5d5 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1082,6 +1082,7 @@ Reshaping - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) - Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`) - Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) +- Issue warning if value column name already exists when using :meth:`DataFrame.melt` (:issue:`34731`) Sparse ^^^^^^ From f20e90f356ebf987beaca4210b221eacc3d83dd9 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:28:38 +1000 Subject: [PATCH 04/23] flake8ified my patch --- pandas/tests/reshape/test_melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 240f6bdd79e2e..eed30b8ef2a5d 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1024,7 +1024,7 @@ def test_warn_of_column_name_value(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars="value") + dfm = df.melt(id_vars="value") # noqa F841 assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "conflicts" in str(w[-1].message) From ff84c46ef3df05be2e03267a2210894cf2246a38 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:55:16 +1000 Subject: [PATCH 05/23] enacted isortification on patch --- pandas/core/reshape/melt.py | 6 +++--- pandas/tests/reshape/test_melt.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 4b31b0bc39f19..f44f577f0e546 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -1,8 +1,8 @@ import re from typing import TYPE_CHECKING, List, cast +import warnings import numpy as np -import warnings from pandas.util._decorators import Appender, deprecate_kwarg @@ -43,8 +43,8 @@ def melt( if value_name in frame.columns: warnings.warn( - 'The value column name "%s" conflicts with an existing' - " column in the dataframe." % (value_name), + 'The value column name "%s" conflicts with an existing ' + "column in the dataframe." % (value_name), DeprecationWarning, ) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index eed30b8ef2a5d..7793f1a96114d 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1,10 +1,11 @@ +import warnings + import numpy as np import pytest import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm -import warnings class TestMelt: From 4a52ccdbf7c83d7d9fd4d62b37bcfcbc8a2c33a4 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:35:15 +1000 Subject: [PATCH 06/23] warn if value column name already exists --- pandas/core/reshape/melt.py | 6 ++++++ pandas/tests/reshape/test_melt.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index cd0619738677d..53b8378490046 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, List, cast import numpy as np +import warnings from pandas.util._decorators import Appender, deprecate_kwarg @@ -40,6 +41,11 @@ def melt( else: cols = list(frame.columns) + if value_name in frame.columns: + warnings.warn('The value column name "%s" conflicts with an existing' + ' column in the dataframe.' % (value_name), + DeprecationWarning) + if id_vars is not None: if not is_list_like(id_vars): id_vars = [id_vars] diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 000a6354277ab..00d9e58c9be73 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm +import warnings class TestMelt: @@ -1014,3 +1015,18 @@ def test_col_substring_of_stubname(self): ) result = pd.wide_to_long(wide_df, stubnames="PA", i=["node_id", "A"], j="time") tm.assert_frame_equal(result, expected) + + def test_warn_of_column_name_value(self): + # GH34731 + # raise a warning if the resultant value column name matches + # a name in the dataframe already (default name is "value") + df = pd.DataFrame({'col':list('ABC'), + 'value':range(10,16,2)}) + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + dfm = df.melt(id_vars='value') + assert len(w) == 1 + assert issubclass(w[-1].category, DeprecationWarning) + assert "conflicts" in str(w[-1].message) + From e8821d37c7fa3c2ee7c13909d771262f878c6766 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:59:49 +1000 Subject: [PATCH 07/23] enblackenated my patch --- pandas/core/reshape/melt.py | 8 +++++--- pandas/tests/reshape/test_melt.py | 8 +++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 53b8378490046..3f2849fb00d4c 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -42,9 +42,11 @@ def melt( cols = list(frame.columns) if value_name in frame.columns: - warnings.warn('The value column name "%s" conflicts with an existing' - ' column in the dataframe.' % (value_name), - DeprecationWarning) + warnings.warn( + 'The value column name "%s" conflicts with an existing' + " column in the dataframe." % (value_name), + DeprecationWarning, + ) if id_vars is not None: if not is_list_like(id_vars): diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 00d9e58c9be73..240f6bdd79e2e 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1020,13 +1020,11 @@ def test_warn_of_column_name_value(self): # GH34731 # raise a warning if the resultant value column name matches # a name in the dataframe already (default name is "value") - df = pd.DataFrame({'col':list('ABC'), - 'value':range(10,16,2)}) + df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars='value') + dfm = df.melt(id_vars="value") assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) - assert "conflicts" in str(w[-1].message) - + assert "conflicts" in str(w[-1].message) From c7dd8de2403c6a895533e8218a7674260ed8e6fd Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:19:30 +1000 Subject: [PATCH 08/23] added entry in whatsnew file --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c5eb2febe8ae9..b7562c7058e47 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -1105,6 +1105,7 @@ Reshaping - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) - Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`) - Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) +- Issue warning if value column name already exists when using :meth:`DataFrame.melt` (:issue:`34731`) Sparse ^^^^^^ From 0dc55f31ca02045dd4accff9c82a782592387200 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:28:38 +1000 Subject: [PATCH 09/23] flake8ified my patch --- pandas/tests/reshape/test_melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 240f6bdd79e2e..eed30b8ef2a5d 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1024,7 +1024,7 @@ def test_warn_of_column_name_value(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars="value") + dfm = df.melt(id_vars="value") # noqa F841 assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "conflicts" in str(w[-1].message) From 4b8cbc0e1292b052325cdbc2e90f3186b18acfda Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:55:16 +1000 Subject: [PATCH 10/23] enacted isortification on patch --- pandas/core/reshape/melt.py | 6 +++--- pandas/tests/reshape/test_melt.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 3f2849fb00d4c..1c1fa824cc29a 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -1,8 +1,8 @@ import re from typing import TYPE_CHECKING, List, cast +import warnings import numpy as np -import warnings from pandas.util._decorators import Appender, deprecate_kwarg @@ -43,8 +43,8 @@ def melt( if value_name in frame.columns: warnings.warn( - 'The value column name "%s" conflicts with an existing' - " column in the dataframe." % (value_name), + 'The value column name "%s" conflicts with an existing ' + "column in the dataframe." % (value_name), DeprecationWarning, ) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index eed30b8ef2a5d..7793f1a96114d 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1,10 +1,11 @@ +import warnings + import numpy as np import pytest import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm -import warnings class TestMelt: From 720713df0851f9e962f5a2a690da99f35434c362 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:35:15 +1000 Subject: [PATCH 11/23] warn if value column name already exists --- pandas/core/reshape/melt.py | 1 + pandas/tests/reshape/test_melt.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 1c1fa824cc29a..3dfdb794488f7 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -3,6 +3,7 @@ import warnings import numpy as np +import warnings from pandas.util._decorators import Appender, deprecate_kwarg diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 7793f1a96114d..889c60feb7b5b 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -6,6 +6,7 @@ import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm +import warnings class TestMelt: @@ -1021,11 +1022,12 @@ def test_warn_of_column_name_value(self): # GH34731 # raise a warning if the resultant value column name matches # a name in the dataframe already (default name is "value") - df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) + df = pd.DataFrame({'col':list('ABC'), + 'value':range(10,16,2)}) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars="value") # noqa F841 + dfm = df.melt(id_vars='value') assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) - assert "conflicts" in str(w[-1].message) + assert "conflicts" in str(w[-1].message) From f68e32257cd2590ac20b55af0196f14c1395d055 Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 12:59:49 +1000 Subject: [PATCH 12/23] enblackenated my patch --- pandas/tests/reshape/test_melt.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 889c60feb7b5b..7db79aaba1ba9 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1022,12 +1022,11 @@ def test_warn_of_column_name_value(self): # GH34731 # raise a warning if the resultant value column name matches # a name in the dataframe already (default name is "value") - df = pd.DataFrame({'col':list('ABC'), - 'value':range(10,16,2)}) + df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars='value') + dfm = df.melt(id_vars="value") assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "conflicts" in str(w[-1].message) From 77003a9afa3cbc802a9d6ee8ed7da8d996a99d3f Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:28:38 +1000 Subject: [PATCH 13/23] flake8ified my patch --- pandas/tests/reshape/test_melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 7db79aaba1ba9..b8379501dc3b8 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1026,7 +1026,7 @@ def test_warn_of_column_name_value(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - dfm = df.melt(id_vars="value") + dfm = df.melt(id_vars="value") # noqa F841 assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) assert "conflicts" in str(w[-1].message) From c9efcee3ec66e9491a65cbc0da3c5f8cda13bf2e Mon Sep 17 00:00:00 2001 From: jason king Date: Fri, 26 Jun 2020 13:55:16 +1000 Subject: [PATCH 14/23] enacted isortification on patch --- pandas/core/reshape/melt.py | 1 - pandas/tests/reshape/test_melt.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 3dfdb794488f7..1c1fa824cc29a 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -3,7 +3,6 @@ import warnings import numpy as np -import warnings from pandas.util._decorators import Appender, deprecate_kwarg diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index b8379501dc3b8..95af2696dfc74 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -6,7 +6,6 @@ import pandas as pd from pandas import DataFrame, lreshape, melt, wide_to_long import pandas._testing as tm -import warnings class TestMelt: From e120f2304014398cb4b5c067db8501d1e7aeb172 Mon Sep 17 00:00:00 2001 From: jason king Date: Sat, 27 Jun 2020 13:48:49 +1000 Subject: [PATCH 15/23] replaced pytest.warns with assert_produces_warning as requested --- pandas/tests/reshape/test_melt.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 95af2696dfc74..25f3022d29583 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1023,9 +1023,5 @@ def test_warn_of_column_name_value(self): # a name in the dataframe already (default name is "value") df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(DeprecationWarning): dfm = df.melt(id_vars="value") # noqa F841 - assert len(w) == 1 - assert issubclass(w[-1].category, DeprecationWarning) - assert "conflicts" in str(w[-1].message) From a9da61e597b904604d9e27ef786bade0f8a5a6cf Mon Sep 17 00:00:00 2001 From: jason king Date: Sat, 27 Jun 2020 14:00:04 +1000 Subject: [PATCH 16/23] had to set stacklevel --- pandas/core/reshape/melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 1c1fa824cc29a..13ca5dfd86275 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -45,7 +45,7 @@ def melt( warnings.warn( 'The value column name "%s" conflicts with an existing ' "column in the dataframe." % (value_name), - DeprecationWarning, + DeprecationWarning, stacklevel=3 ) if id_vars is not None: From 56d2accc636c8fbb87066b76aa605b8c69281afa Mon Sep 17 00:00:00 2001 From: jason king Date: Sat, 27 Jun 2020 14:37:27 +1000 Subject: [PATCH 17/23] Gwqot a warning about not removing warnings after I removed a use of a warning detection. Warning. --- pandas/tests/reshape/test_melt.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 25f3022d29583..878f98c45808a 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest From 9ff17c4278338996fb6b780e1f7b30a1d717b104 Mon Sep 17 00:00:00 2001 From: jason king Date: Sat, 27 Jun 2020 15:02:59 +1000 Subject: [PATCH 18/23] reenblackenatified file. --- pandas/core/reshape/melt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 13ca5dfd86275..25950b1a52f4a 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -45,7 +45,8 @@ def melt( warnings.warn( 'The value column name "%s" conflicts with an existing ' "column in the dataframe." % (value_name), - DeprecationWarning, stacklevel=3 + DeprecationWarning, + stacklevel=3, ) if id_vars is not None: From 2d55f27589c3996cfb3c188882c9bbe9b7731f6c Mon Sep 17 00:00:00 2001 From: jason king Date: Tue, 30 Jun 2020 14:38:12 +1000 Subject: [PATCH 19/23] requested changes --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/reshape/melt.py | 8 +++++--- pandas/tests/reshape/test_melt.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index b7562c7058e47..e2650444667c1 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -816,6 +816,7 @@ Deprecations - :meth:`util.testing.assert_almost_equal` now accepts both relative and absolute precision through the ``rtol``, and ``atol`` parameters, thus deprecating the ``check_less_precise`` parameter. (:issue:`13357`). +- Issue warning if value column name already exists when using :meth:`DataFrame.melt` (:issue:`34731`) .. --------------------------------------------------------------------------- @@ -1105,7 +1106,6 @@ Reshaping - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) - Bug in :meth:`Series.where` with an empty Series and empty ``cond`` having non-bool dtype (:issue:`34592`) - Fixed regression where :meth:`DataFrame.apply` would raise ``ValueError`` for elements whth ``S`` dtype (:issue:`34529`) -- Issue warning if value column name already exists when using :meth:`DataFrame.melt` (:issue:`34731`) Sparse ^^^^^^ diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 25950b1a52f4a..bf5395f603fac 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -43,9 +43,11 @@ def melt( if value_name in frame.columns: warnings.warn( - 'The value column name "%s" conflicts with an existing ' - "column in the dataframe." % (value_name), - DeprecationWarning, + 'This dataframe has a column name that matches the value column ' + f'name of the resultant melted dataframe (That being "{value_name})". ' + 'In the future this will raise an error, please set the value_name ' + 'parameter of DataFrame.melt to a unique name.', + FutureWarning, stacklevel=3, ) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 878f98c45808a..212b0c7d1ab96 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1021,5 +1021,5 @@ def test_warn_of_column_name_value(self): # a name in the dataframe already (default name is "value") df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) - with tm.assert_produces_warning(DeprecationWarning): + with tm.assert_produces_warning(FutureWarning): dfm = df.melt(id_vars="value") # noqa F841 From d118a90889209bffa827d5fd02114f05b55d1168 Mon Sep 17 00:00:00 2001 From: jason king Date: Tue, 30 Jun 2020 15:12:59 +1000 Subject: [PATCH 20/23] black says "no" to single quote strings. --- pandas/core/reshape/melt.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index bf5395f603fac..bc27192ef1ef5 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -43,10 +43,10 @@ def melt( if value_name in frame.columns: warnings.warn( - 'This dataframe has a column name that matches the value column ' + "This dataframe has a column name that matches the value column " f'name of the resultant melted dataframe (That being "{value_name})". ' - 'In the future this will raise an error, please set the value_name ' - 'parameter of DataFrame.melt to a unique name.', + "In the future this will raise an error, please set the value_name " + "parameter of DataFrame.melt to a unique name.", FutureWarning, stacklevel=3, ) From ffaa00c285be006ec15da150caba7c6060893092 Mon Sep 17 00:00:00 2001 From: jason king Date: Wed, 1 Jul 2020 22:21:53 +1000 Subject: [PATCH 21/23] made further requested changes. --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/reshape/melt.py | 6 +++--- pandas/tests/reshape/test_melt.py | 7 ++++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e2650444667c1..009e8ec3eb52b 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -816,7 +816,7 @@ Deprecations - :meth:`util.testing.assert_almost_equal` now accepts both relative and absolute precision through the ``rtol``, and ``atol`` parameters, thus deprecating the ``check_less_precise`` parameter. (:issue:`13357`). -- Issue warning if value column name already exists when using :meth:`DataFrame.melt` (:issue:`34731`) +- :func:`DataFrame.melt` accepting a value_name that already exists is deprecated, and will be removed in a future version (:issue:`34731`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index bc27192ef1ef5..923b9e7462d8b 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -43,9 +43,9 @@ def melt( if value_name in frame.columns: warnings.warn( - "This dataframe has a column name that matches the value column " - f'name of the resultant melted dataframe (That being "{value_name})". ' - "In the future this will raise an error, please set the value_name " + "This dataframe has a column name that matches the 'value_name' column " + "name of the resultiing Dataframe. " + "In the future this will raise an error, please set the 'value_name' " "parameter of DataFrame.melt to a unique name.", FutureWarning, stacklevel=3, diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 212b0c7d1ab96..9cece4b3b8902 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1020,6 +1020,11 @@ def test_warn_of_column_name_value(self): # raise a warning if the resultant value column name matches # a name in the dataframe already (default name is "value") df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) + expected = pd.DataFrame( + [['A','col','A'],['B','col','B'],['C','col','C']], + columns = ['value', 'variable', 'value'] + ) with tm.assert_produces_warning(FutureWarning): - dfm = df.melt(id_vars="value") # noqa F841 + result = df.melt(id_vars="value") + tm.assert_frame_equal(result, expected) From d2b9fdd685482495f62c68aaf05a4064ddbc0586 Mon Sep 17 00:00:00 2001 From: jason king Date: Thu, 2 Jul 2020 00:07:51 +1000 Subject: [PATCH 22/23] E231 formatting problems fixed --- pandas/tests/reshape/test_melt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 9cece4b3b8902..f1b569abad47a 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1021,8 +1021,8 @@ def test_warn_of_column_name_value(self): # a name in the dataframe already (default name is "value") df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) expected = pd.DataFrame( - [['A','col','A'],['B','col','B'],['C','col','C']], - columns = ['value', 'variable', 'value'] + [["A", "col", "A"], ["B", "col", "B"], ["C", "col", "C"]], + columns=["value", "variable", "value"] ) with tm.assert_produces_warning(FutureWarning): From b8d79f46813d4f5476d78b86451b5b7c6faf0dd7 Mon Sep 17 00:00:00 2001 From: jason king Date: Thu, 2 Jul 2020 23:33:41 +1000 Subject: [PATCH 23/23] ok black, you can have the comma. --- pandas/tests/reshape/test_melt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index f1b569abad47a..a0fa10802f860 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -1022,7 +1022,7 @@ def test_warn_of_column_name_value(self): df = pd.DataFrame({"col": list("ABC"), "value": range(10, 16, 2)}) expected = pd.DataFrame( [["A", "col", "A"], ["B", "col", "B"], ["C", "col", "C"]], - columns=["value", "variable", "value"] + columns=["value", "variable", "value"], ) with tm.assert_produces_warning(FutureWarning):