Skip to content

Commit 49390b9

Browse files
author
Chang She
committed
BUG: remove unused columns from final result
1 parent be58573 commit 49390b9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pandas/core/reshape.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pandas.algos as algos
1818

1919

20-
from pandas.core.index import MultiIndex
20+
from pandas.core.index import MultiIndex, Index
2121

2222

2323
class ReshapeError(Exception):
@@ -506,12 +506,14 @@ def _stack_multi_columns(frame, level=-1, dropna=True):
506506
new_data = {}
507507
level_vals = this.columns.levels[-1]
508508
levsize = len(level_vals)
509+
drop_cols = []
509510
for key in unique_groups:
510511
loc = this.columns.get_loc(key)
511512
slice_len = loc.stop - loc.start
512513
# can make more efficient?
513514

514515
if slice_len == 0:
516+
drop_cols.append(key)
515517
continue
516518
elif slice_len != levsize:
517519
chunk = this.ix[:, this.columns[loc]]
@@ -525,6 +527,9 @@ def _stack_multi_columns(frame, level=-1, dropna=True):
525527

526528
new_data[key] = value_slice.ravel()
527529

530+
if len(drop_cols) > 0:
531+
new_columns = new_columns - drop_cols
532+
528533
N = len(this)
529534

530535
if isinstance(this.index, MultiIndex):

pandas/tests/test_multilevel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,9 @@ def test_stack_multiple_bug(self):
942942
unst = multi.unstack('ID')
943943
down = unst.resample('W-THU')
944944

945-
rs = down.stack('ID') # It works!
946-
945+
rs = down.stack('ID')
946+
xp = unst.ix[:, ['VAR1']].resample('W-THU').stack('ID')
947+
assert_frame_equal(rs, xp)
947948

948949
def test_unstack_multiple_hierarchical(self):
949950
df = DataFrame(index=[[0, 0, 0, 0, 1, 1, 1, 1],

0 commit comments

Comments
 (0)