Skip to content

Commit 0a04723

Browse files
committed
BUG: fix unstacking edge case failure when filtering unrepresented combinations. close #2100
1 parent 37868c4 commit 0a04723

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pandas 0.9.1
4141
- Make WLS r-squared match statsmodels 0.5.0 fixed value
4242
- Fix zero-trimming DataFrame formatting bug
4343
- Correctly compute/box datetime64 min/max values from Series.min/max (#2083)
44+
- Fix unstacking edge case with unrepresented groups (#2100)
4445

4546
pandas 0.9.0
4647
============

pandas/core/reshape.py

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def get_new_values(self):
169169
mask_chunk.flat[self.mask] = True
170170

171171
new_values = new_values.take(self.unique_groups, axis=0)
172+
new_mask = new_mask.take(self.unique_groups, axis=0)
172173

173174
return new_values, new_mask
174175

pandas/tests/test_multilevel.py

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pandas.core.common as com
1616
import pandas.util.testing as tm
1717
from pandas.util.compat import product as cart_product
18+
import pandas as pd
1819

1920
class TestMultiLevel(unittest.TestCase):
2021

@@ -711,6 +712,28 @@ def test_stack(self):
711712
result = self.ymd.unstack(0).stack(-2)
712713
expected = self.ymd.unstack(0).stack(0)
713714

715+
def test_unstack_odd_failure(self):
716+
data = """day,time,smoker,sum,len
717+
Fri,Dinner,No,8.25,3.
718+
Fri,Dinner,Yes,27.03,9
719+
Fri,Lunch,No,3.0,1
720+
Fri,Lunch,Yes,13.68,6
721+
Sat,Dinner,No,139.63,45
722+
Sat,Dinner,Yes,120.77,42
723+
Sun,Dinner,No,180.57,57
724+
Sun,Dinner,Yes,66.82,19
725+
Thur,Dinner,No,3.0,1
726+
Thur,Lunch,No,117.32,44
727+
Thur,Lunch,Yes,51.51,17"""
728+
729+
df = pd.read_csv(StringIO(data)).set_index(['day', 'time', 'smoker'])
730+
731+
# it works, #2100
732+
result = df.unstack(2)
733+
734+
recons = result.stack()
735+
assert_frame_equal(recons, df)
736+
714737
def test_stack_mixed_dtype(self):
715738
df = self.frame.T
716739
df['foo', 'four'] = 'foo'

0 commit comments

Comments
 (0)