Skip to content

Commit a307b44

Browse files
Andyproost
Andy
authored andcommitted
add unit tests for issue pandas-dev#19351 (pandas-dev#29438)
1 parent cd2fa32 commit a307b44

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pandas/tests/test_multilevel.py

+43
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,49 @@ def test_unstack(self):
358358
# test that int32 work
359359
self.ymd.astype(np.int32).unstack()
360360

361+
@pytest.mark.parametrize(
362+
"result_rows,result_columns,index_product,expected_row",
363+
[
364+
(
365+
[[1, 1, None, None, 30.0, None], [2, 2, None, None, 30.0, None]],
366+
[u"ix1", u"ix2", u"col1", u"col2", u"col3", u"col4"],
367+
2,
368+
[None, None, 30.0, None],
369+
),
370+
(
371+
[[1, 1, None, None, 30.0], [2, 2, None, None, 30.0]],
372+
[u"ix1", u"ix2", u"col1", u"col2", u"col3"],
373+
2,
374+
[None, None, 30.0],
375+
),
376+
(
377+
[[1, 1, None, None, 30.0], [2, None, None, None, 30.0]],
378+
[u"ix1", u"ix2", u"col1", u"col2", u"col3"],
379+
None,
380+
[None, None, 30.0],
381+
),
382+
],
383+
)
384+
def test_unstack_partial(
385+
self, result_rows, result_columns, index_product, expected_row
386+
):
387+
# check for regressions on this issue:
388+
# https://github.com/pandas-dev/pandas/issues/19351
389+
# make sure DataFrame.unstack() works when its run on a subset of the DataFrame
390+
# and the Index levels contain values that are not present in the subset
391+
result = pd.DataFrame(result_rows, columns=result_columns).set_index(
392+
[u"ix1", "ix2"]
393+
)
394+
result = result.iloc[1:2].unstack("ix2")
395+
expected = pd.DataFrame(
396+
[expected_row],
397+
columns=pd.MultiIndex.from_product(
398+
[result_columns[2:], [index_product]], names=[None, "ix2"]
399+
),
400+
index=pd.Index([2], name="ix1"),
401+
)
402+
tm.assert_frame_equal(result, expected)
403+
361404
def test_unstack_multiple_no_empty_columns(self):
362405
index = MultiIndex.from_tuples(
363406
[(0, "foo", 0), (0, "bar", 0), (1, "baz", 1), (1, "qux", 1)]

0 commit comments

Comments
 (0)