-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.melt gives unexpected result when column "value" already exists #35003
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
Changes from 23 commits
5a84593
c00363e
d243026
968fc5b
12bdaf7
f20e90f
ff84c46
7267b59
98acc00
4a52ccd
e8821d3
c7dd8de
0dc55f3
4b8cbc0
720713d
f68e322
77003a9
c9efcee
e120f23
a9da61e
6a7ad2e
56d2acc
9ff17c4
2d55f27
d118a90
ffaa00c
d2b9fdd
b8d79f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import re | ||
from typing import TYPE_CHECKING, List, cast | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
|
@@ -40,6 +41,14 @@ def melt( | |
else: | ||
cols = list(frame.columns) | ||
|
||
if value_name in frame.columns: | ||
warnings.warn( | ||
'The value column name "%s" conflicts with an existing ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use f-string syntax? We don't use the Py2 syntax any more |
||
"column in the dataframe." % (value_name), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally we want to give users insight as to what they need to do before the deprecated behavior gets changed - can you try to clarify the message a bit? |
||
DeprecationWarning, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to a FutureWarning? |
||
stacklevel=3, | ||
) | ||
|
||
if id_vars is not None: | ||
if not is_list_like(id_vars): | ||
id_vars = [id_vars] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1014,3 +1014,12 @@ 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 tm.assert_produces_warning(DeprecationWarning): | ||
dfm = df.melt(id_vars="value") # noqa F841 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you create an expected value and use assert_frame_equal also instead of dfm, call this result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to the Deprecations section?