Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pandas-dev/pandas
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2dc6cab9a6f3e15aad10dafa5da241f463c6c22a
Choose a base ref
..
head repository: pandas-dev/pandas
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a629b82cbec104c0fe12fd7439f2627cae1a6ea2
Choose a head ref
Showing with 6 additions and 4 deletions.
  1. +2 −1 pandas/core/reshape/reshape.py
  2. +4 −3 pandas/tests/frame/test_reshape.py
3 changes: 2 additions & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ def get_new_columns(self):
new_names = [self.value_columns.name, self.removed_name]
new_labels = [propagator]

# The two indices differ iff the unstacked level had unused items.
# The two indices differ only if the unstacked level had unused items:
if len(self.removed_level_full) != len(self.removed_level):
# In this case, we remap the new labels to the original level:
repeater = self.removed_level_full.get_indexer(self.removed_level)
@@ -262,6 +262,7 @@ def get_new_columns(self):
else:
# Otherwise, we just use each level item exactly once:
repeater = np.arange(stride) - self.lift

# The entire level is then just a repetition of the single chunk:
new_labels.append(np.tile(repeater, width))
return MultiIndex(levels=new_levels, labels=new_labels,
7 changes: 4 additions & 3 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
@@ -561,7 +561,7 @@ def test_unstack_dtypes(self):
tm.assert_frame_equal(left, right)

def test_unstack_unused_levels(self):
# GH 17845: sliced columns of int DataFrame
# GH 17845: unused labels in index make unstack() cast int to float
idx = pd.MultiIndex.from_product([['a'], ['A', 'B', 'C', 'D']])[:-1]
df = pd.DataFrame([[1, 0]] * 3, index=idx)

@@ -579,9 +579,10 @@ def test_unstack_unused_levels(self):
block = np.arange(4).reshape(2, 2)
df = pd.DataFrame(np.concatenate([block, block + 4]), index=idx)
result = df.unstack()
expected = pd.DataFrame(np.concatenate([block * 2, block * 2 - 1],
expected = pd.DataFrame(np.concatenate([block * 2, block * 2 + 1],
axis=1),
columns=idx)
tm.assert_frame_equal(result, expected)
assert((result.columns.levels[1] == idx.levels[1]).all())

# With mixed dtype and NaN
@@ -625,7 +626,7 @@ def test_unstack_unused_level(self, cols):
expected.columns = MultiIndex.from_product([expected.columns, ['I']],
names=[None, 'C'])
expected.index = expected.index.droplevel('C')
assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected)

def test_unstack_nan_index(self): # GH7466
cast = lambda val: '{0:1}'.format('' if val != val else val)