Skip to content
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 #34731

Closed
2 of 3 tasks
erfannariman opened this issue Jun 12, 2020 · 4 comments · Fixed by #35003
Closed
2 of 3 tasks

BUG: DataFrame.melt gives unexpected result when column "value" already exists #34731

erfannariman opened this issue Jun 12, 2020 · 4 comments · Fixed by #35003
Labels
Bug Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@erfannariman
Copy link
Member

erfannariman commented Jun 12, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

df = pd.DataFrame({'col':list('ABC'),
                   'value':range(10,16,2)})
print(df, '\n')

dfm = df.melt(id_vars='value')

print(dfm)

  col  value
0   A     10
1   B     12
2   C     14 

  value variable value
0     A      col     A
1     B      col     B
2     C      col     C

Problem description

When the column value already exists, and we set it as index with id_vars it copies the value column of the melted dataframe and does not set the "old" value column of the original dataframe as id_vars

Expected Output

# note change of column name 'val' instead of 'value'
df = pd.DataFrame({'col':list('ABC'),
                   'val':range(10,16,2)})

dfm = df.melt(id_vars='val')

print(dfm)
   val variable value
0   10      col     A
1   12      col     B
2   14      col     C

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 47.1.1.post20200604
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@erfannariman erfannariman added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 12, 2020
@TomAugspurger
Copy link
Contributor

Hmm I don't agree with your expected output. We wouldn't want to change the name of the old one.

I'd prefer that we raise if we detect that we're going to overwrite a value / introduce duplicate labels, unless we document the current behavior.

@TomAugspurger TomAugspurger added Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 12, 2020
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Jun 12, 2020
@erfannariman
Copy link
Member Author

Yes, I agree, it should at the minimum give a warning.

@erfannariman
Copy link
Member Author

@TomAugspurger as someone having hard time to get started with contributing, would this be a good one to try?

@TomAugspurger
Copy link
Contributor

Not sure. This will be an API change so I think we'll need to warn first, and then in 2.x we can raise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants