-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: avoid unnecessary casting when unstacking index with unused levels #18460
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 all commits
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 |
---|---|---|
|
@@ -560,6 +560,74 @@ def test_unstack_dtypes(self): | |
assert left.shape == (3, 2) | ||
tm.assert_frame_equal(left, right) | ||
|
||
def test_unstack_unused_levels(self): | ||
# 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) | ||
|
||
result = df.unstack() | ||
exp_col = pd.MultiIndex.from_product([[0, 1], ['A', 'B', 'C']]) | ||
expected = pd.DataFrame([[1, 1, 1, 0, 0, 0]], index=['a'], | ||
columns=exp_col) | ||
tm.assert_frame_equal(result, expected) | ||
assert((result.columns.levels[1] == idx.levels[1]).all()) | ||
|
||
# Unused items on both levels | ||
levels = [[0, 1, 7], [0, 1, 2, 3]] | ||
labels = [[0, 0, 1, 1], [0, 2, 0, 2]] | ||
idx = pd.MultiIndex(levels, labels) | ||
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], | ||
axis=1), | ||
columns=idx) | ||
tm.assert_frame_equal(result, expected) | ||
assert((result.columns.levels[1] == idx.levels[1]).all()) | ||
|
||
# With mixed dtype and NaN | ||
levels = [['a', 2, 'c'], [1, 3, 5, 7]] | ||
labels = [[0, -1, 1, 1], [0, 2, -1, 2]] | ||
idx = pd.MultiIndex(levels, labels) | ||
data = np.arange(8) | ||
df = pd.DataFrame(data.reshape(4, 2), index=idx) | ||
|
||
cases = ((0, [13, 16, 6, 9, 2, 5, 8, 11], | ||
[np.nan, 'a', 2], [np.nan, 5, 1]), | ||
(1, [8, 11, 1, 4, 12, 15, 13, 16], | ||
[np.nan, 5, 1], [np.nan, 'a', 2])) | ||
for level, idces, col_level, idx_level in cases: | ||
result = df.unstack(level=level) | ||
exp_data = np.zeros(18) * np.nan | ||
exp_data[idces] = data | ||
cols = pd.MultiIndex.from_product([[0, 1], col_level]) | ||
expected = pd.DataFrame(exp_data.reshape(3, 6), | ||
index=idx_level, columns=cols) | ||
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. can we have an exact expected frame and assert_frame_equal (maybe more code, but it really locks it down to the exact result). 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. The frame is an exact copy, but 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.
(shall I?) 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 think worthwhile to fix #18455 first actually. 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. |
||
# Broken (GH 18455): | ||
# tm.assert_frame_equal(result, expected) | ||
diff = result - expected | ||
assert(diff.sum().sum() == 0) | ||
assert((diff + 1).sum().sum() == 8) | ||
|
||
assert((result.columns.levels[1] == idx.levels[level]).all()) | ||
|
||
@pytest.mark.parametrize("cols", [['A', 'C'], slice(None)]) | ||
def test_unstack_unused_level(self, cols): | ||
# GH 18562 : unused labels on the unstacked level | ||
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 believe you had 2 cases for #18562 does this cover both? 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. yes, they are the two "cols" values |
||
df = pd.DataFrame([[2010, 'a', 'I'], | ||
[2011, 'b', 'II']], | ||
columns=['A', 'B', 'C']) | ||
|
||
ind = df.set_index(['A', 'B', 'C'], drop=False) | ||
selection = ind.loc[(slice(None), slice(None), 'I'), cols] | ||
result = selection.unstack() | ||
|
||
expected = ind.iloc[[0]][cols] | ||
expected.columns = MultiIndex.from_product([expected.columns, ['I']], | ||
names=[None, 'C']) | ||
expected.index = expected.index.droplevel('C') | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_unstack_nan_index(self): # GH7466 | ||
cast = lambda val: '{0:1}'.format('' if val != val else val) | ||
nan = np.nan | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a comment on what is going on here (e.g. the unsused bizness)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see below)