Skip to content

add unit tests for issue #19351 #29438

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

Merged
merged 3 commits into from
Nov 6, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,53 @@ def test_unstack(self):
# test that int32 work
self.ymd.astype(np.int32).unstack()

def test_unstack_partial(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally would like to parametrize this, have a look at pandas/tests/reshape/merge/test_merge.py for examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. done!

# check for regressions on this issue:
# https://github.com/pandas-dev/pandas/issues/19351
# make sure DataFrame.unstack() works when its run on a subset of the DataFrame
# and the Index levels contain values that are not present in the subset
result1 = pd.DataFrame(
[[1, 1, None, None, 30.0, None], [2, 2, None, None, 30.0, None]],
columns=[u"ix1", u"ix2", u"col1", u"col2", u"col3", u"col4"],
).set_index([u"ix1", "ix2"])
result1 = result1.iloc[1:2].unstack("ix2")
expected1 = pd.DataFrame(
[[None, None, 30.0, None]],
columns=pd.MultiIndex.from_product(
[["col1", "col2", "col3", "col4"], [2]], names=[None, "ix2"]
),
index=pd.Index([2], name="ix1"),
)
tm.assert_frame_equal(result1, expected1)

result2 = pd.DataFrame(
[[1, 1, None, None, 30.0], [2, 2, None, None, 30.0]],
columns=[u"ix1", u"ix2", u"col1", u"col2", u"col3"],
).set_index([u"ix1", "ix2"])
result2 = result2.iloc[1:2].unstack("ix2")
expected2 = pd.DataFrame(
[[None, None, 30.0]],
columns=pd.MultiIndex.from_product(
[["col1", "col2", "col3"], [2]], names=[None, "ix2"]
),
index=pd.Index([2], name="ix1"),
)
tm.assert_frame_equal(result2, expected2)

result3 = pd.DataFrame(
[[1, 1, None, None, 30.0], [2, None, None, None, 30.0]],
columns=[u"ix1", u"ix2", u"col1", u"col2", u"col3"],
).set_index([u"ix1", "ix2"])
result3 = result3.iloc[1:2].unstack("ix2")
expected3 = pd.DataFrame(
[[None, None, 30.0]],
columns=pd.MultiIndex.from_product(
[["col1", "col2", "col3"], [None]], names=[None, "ix2"]
),
index=pd.Index([2], name="ix1"),
)
tm.assert_frame_equal(result3, expected3)

def test_unstack_multiple_no_empty_columns(self):
index = MultiIndex.from_tuples(
[(0, "foo", 0), (0, "bar", 0), (1, "baz", 1), (1, "qux", 1)]
Expand Down