-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add optional argument index to pd.melt to maintain index values #33659
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 56 commits
a4f2d22
62564cf
a7a8460
d9be281
8bf8c78
cd95dce
7a7390d
248ac12
6ea78de
44c5439
92c98cd
64fa6f3
cb276e8
4ebcb48
71436a0
00cc66b
4c81853
e85da03
2348c3b
d02a5a5
c71e021
ae3f9ea
a410e51
7c9dcce
4bd8400
43ab0fe
024492a
b424c8e
571489b
dad4a53
9b6bc9a
b92348e
c2cbfd7
799bb28
b97c4ae
10c7cde
594fea4
dda5657
1233b9e
0f45e4d
9e8eaac
bb179b7
4161ede
7ae7261
7cee87d
0455f64
d5fb84e
d65e836
f782fee
14062aa
39bd069
0bc198c
e67c842
666a856
250fa5e
fc7e50b
f3b1dca
8316cbf
2b6ec46
7ac9aa5
118a15d
64afbd0
a1ecb46
551e40f
a462afd
b1bf46d
368bfa5
bf7d5e5
9008ccc
a6ec490
0391b7a
800c050
788c28a
e134ed2
7a765a3
b1cca84
c66767d
7f5018f
16e9bd4
df645e1
bbf8465
57bffd1
edcd123
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import pandas.core.common as com | ||
from pandas.core.frame import DataFrame, _shared_docs | ||
from pandas.core.indexes.base import Index | ||
from pandas.core.indexes.multi import MultiIndex | ||
from pandas.core.reshape.concat import concat | ||
from pandas.core.tools.numeric import to_numeric | ||
|
||
|
@@ -29,8 +30,8 @@ def melt( | |
var_name=None, | ||
value_name="value", | ||
col_level=None, | ||
keep_index=False, | ||
) -> DataFrame: | ||
# TODO: what about the existing index? | ||
# If multiindex, gather names of columns on all level for checking presence | ||
# of `id_vars` and `value_vars` | ||
if isinstance(frame.columns, ABCMultiIndex): | ||
|
@@ -115,7 +116,19 @@ def melt( | |
# asanyarray will keep the columns as an Index | ||
mdata[col] = np.asanyarray(frame.columns._get_level_values(i)).repeat(N) | ||
|
||
return frame._constructor(mdata, columns=mcolumns) | ||
result = frame._constructor(mdata, columns=mcolumns) | ||
|
||
if keep_index: | ||
new_index = np.tile(frame.index, K) | ||
Rik-de-Kort marked this conversation as resolved.
Show resolved
Hide resolved
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. both MI and Index already have a .repeat() method, I think we could add a .tile() method to make this easier. (or just use repeat) 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.
I tried having a look at implementing 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. ok fair enough 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. we already have _tile_compat in pandas\core\reshape\util.py. This may allow futher simplification here. 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. This converts to object dtype. Can you use 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. You should be able to remove the next section as well, since you won't be converting to an ndarray. 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. Thanks, that simplifies the code a lot! Build is failing but that's due to a worker crashing. |
||
|
||
if isinstance(frame.index, MultiIndex): | ||
new_index = MultiIndex.from_tuples(new_index, names=frame.index.names) | ||
else: | ||
new_index = Index(new_index, dtype=frame.index.dtype, name=frame.index.name) | ||
|
||
result = result.set_index(new_index) | ||
Rik-de-Kort marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return result | ||
|
||
|
||
@deprecate_kwarg(old_arg_name="label", new_arg_name=None) | ||
|
Uh oh!
There was an error while loading. Please reload this page.