-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Conversation
get all the things
should I remove this line as part of this PR? "libreduction" isn't mentioned anywhere else in the file pandas/core/apply.py , but on the other hand, its out of scope |
Can you merge master to fix CI issue? |
pandas/tests/reshape/test_melt.py
Outdated
# 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: |
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 use tm.assert_produces_warning here (e.g., as in L580 of this file)?
…nto issue34731-1
a use of a warning detection. Warning.
pandas/core/reshape/melt.py
Outdated
@@ -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 comment
The 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
pandas/core/reshape/melt.py
Outdated
warnings.warn( | ||
'The value column name "%s" conflicts with an existing ' | ||
"column in the dataframe." % (value_name), | ||
DeprecationWarning, |
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 change this to a FutureWarning?
doc/source/whatsnew/v1.1.0.rst
Outdated
@@ -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`) |
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?
pandas/core/reshape/melt.py
Outdated
if value_name in frame.columns: | ||
warnings.warn( | ||
'The value column name "%s" conflicts with an existing ' | ||
"column in the dataframe." % (value_name), |
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.
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?
doc/source/whatsnew/v1.1.0.rst
Outdated
@@ -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`) |
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.
pandas/core/reshape/melt.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
put value
in single quotes as this is an argument that the user passed. (and this should be value_name
)
pandas/core/reshape/melt.py
Outdated
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})". ' |
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.
resultant -> resuling DataFrame. you don't need the parenthesized expression.
pandas/core/reshape/melt.py
Outdated
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 " |
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.
value_name
in single quotes
pandas/tests/reshape/test_melt.py
Outdated
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 comment
The 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.
thanks @pizzathief |
@pizzathief want to make a PR to enforce this deprecation? |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff