Skip to content

Commit fda5012

Browse files
jtorcassojreback
authored andcommitted
BUG: wide_to_long no longer changes the stubnames passed as an argument to the function call (GH9204)
1 parent 0523e7c commit fda5012

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,4 @@ Bug Fixes
196196

197197
- Fixed issue in the ``xlsxwriter`` engine where it added a default 'General' format to cells if no other format wass applied. This prevented other row or column formatting being applied. (:issue:`9167`)
198198
- Fixes issue with ``index_col=False`` when ``usecols`` is also specified in ``read_csv``. (:issue:`9082`)
199+
- Bug where ``wide_to_long`` would modify the input stubnames list (:issue:`9204`)

pandas/core/reshape.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,9 @@ def melt_stub(df, stub, i, j):
930930
if i not in id_vars:
931931
id_vars += [i]
932932

933-
stub = stubnames.pop(0)
934-
newdf = melt_stub(df, stub, id_vars, j)
933+
newdf = melt_stub(df, stubnames[0], id_vars, j)
935934

936-
for stub in stubnames:
935+
for stub in stubnames[1:]:
937936
new = melt_stub(df, stub, id_vars, j)
938937
newdf = newdf.merge(new, how="outer", on=id_vars + [j], copy=False)
939938
return newdf.set_index([i, j])

pandas/tests/test_reshape.py

+8
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ def test_simple(self):
443443
long_frame = wide_to_long(df, ["A", "B"], i="id", j="year")
444444
tm.assert_frame_equal(long_frame, exp_frame)
445445

446+
def test_stubs(self):
447+
# GH9204
448+
df = pd.DataFrame([[0,1,2,3,8],[4,5,6,7,9]])
449+
df.columns = ['id', 'inc1', 'inc2', 'edu1', 'edu2']
450+
stubs = ['inc', 'edu']
451+
df_long = pd.wide_to_long(df, stubs, i='id', j='age')
452+
453+
self.assertEqual(stubs,['inc', 'edu'])
446454

447455
if __name__ == '__main__':
448456
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)