-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: DataFrame.melt gives unexpected result when column "value" already exists #35003
Changes from 25 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,16 @@ def melt( | |
else: | ||
cols = list(frame.columns) | ||
|
||
if value_name in frame.columns: | ||
warnings.warn( | ||
"This dataframe has a column name that matches the value column " | ||
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. put |
||
f'name of the resultant melted dataframe (That being "{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. resultant -> resuling DataFrame. you don't need the parenthesized expression. |
||
"In the future this will raise an error, please set the 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.
|
||
"parameter of DataFrame.melt to a unique name.", | ||
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(FutureWarning): | ||
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 start with
DataFrame.melt
(like the other notes above), and also say that this is deprecated and removed in future version.