ENH: Add optional argument index to pd.melt to maintain index values
State: closed

Finishing up a stale PR idea: #28859 and #17459

 Has some tests and better code.
I think it's fair to duplicate the index values and not bend over backwards to maintain uniqueness like in previous iterations.

Apologies for the mess, it was a quick job and I didn't want to spend an hour fiddling with the commits.

Finally, I deleted some ignore type comments for mypy because the commits weren't going on my system. Is there some other fix for that? Other than that I think it's good to go.

- [X] closes #17440
- [X] tests added / passed
- [X] passes `black pandas`
- [X] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [X] whatsnew entry
- [x] Reconsider API design
- [x] Add usage examples in whatsnew, docstring, and reshaping.rst.


Labels:
name: Enhancement
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode
name: API Design

Comments:
Thanks @Rik-de-Kort for the PR.

> Finishing up a stale PR idea, here: #28859

The original work is #17459

Can you review that PR and also address any comments raised there that are relevant to this PR. e.g.

@TomAugspurger wrote https://github.com/pandas-dev/pandas/pull/17459#issuecomment-327818758

> For the docs, it'll need a whatsnew note in doc/source/whatsnew/v0.21.0.txt, prose docs in doc/source/reshaping.rst. It'd also be nice to have an example in the docstring.

can you update reshaping.rst and add examples to docstring. The release note should probably also include examples demonstrating the new functionality.






Hello @Rik-de-Kort! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:











There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-07-07 16:09:51 UTC
Thanks for commenting, @simonjayhawkins I couldn't find that PR when I was looking for it!

* I have renamed the kwarg to `index` rather than `keep_index` as suggested by @jreback.
* I have added an example to the docstring and reshaping.rst.

I don't think the whatsnew needs more prose about the change. It's use is straightforward: as opposed to the previous PR's it does the simplest thing imaginable, namely keep the original index values around in their original form.  
Speaking from an end user perspective (I use pandas a lot in my work), I think this is what I would like the API to be, but of course I'm open to discussion.  
One alternative would be to create a unique index using the old index values and `id_vars` as in the previous PRs. However, that is rather unwieldy in terms of code (it's bad enough to have to handle the case for the multiindex as it is), and also not very consistent with the behaviour when `index=False` because that keeps `id_vars` around as a column rather than as part of the index. 
Some issues with the CI mysteriously failing. Seems resolved now.

@jreback @simonjayhawkins if you have time, could you please review? Of course understandable if priorities lie elsewhere but would like to finish up :)
Does the CI always have this many issues?
@jreback I though I gave you a ping, but I don't see it, so here it is!
thanks @Rik-de-Kort 
You're welcome