Skip to content

Commit

Permalink
BUG: stack with empty level list (#60826)
Browse files Browse the repository at this point in the history
* return early if set_levels is empty

* add test

* add whatsnew

* check empty before make set
  • Loading branch information
quangngd authored Feb 4, 2025
1 parent fc6da9c commit e830603
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ Reshaping
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
- Bug in :meth:`DataFrame.merge` when merging two :class:`DataFrame` on ``intc`` or ``uintc`` types on Windows (:issue:`60091`, :issue:`58713`)
- Bug in :meth:`DataFrame.pivot_table` incorrectly subaggregating results when called without an ``index`` argument (:issue:`58722`)
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)

Sparse
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ def _reorder_for_extension_array_stack(
def stack_v3(frame: DataFrame, level: list[int]) -> Series | DataFrame:
if frame.columns.nunique() != len(frame.columns):
raise ValueError("Columns with duplicate values are not supported in stack")
if not len(level):
return frame
set_levels = set(level)
stack_cols = frame.columns._drop_level_numbers(
[k for k in range(frame.columns.nlevels - 1, -1, -1) if k not in set_levels]
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,25 @@ def test_stack_empty_frame(dropna, future_stack):
tm.assert_series_equal(result, expected)


@pytest.mark.filterwarnings("ignore:The previous implementation of stack is deprecated")
@pytest.mark.parametrize("dropna", [True, False, lib.no_default])
def test_stack_empty_level(dropna, future_stack, int_frame):
# GH 60740
if future_stack and dropna is not lib.no_default:
with pytest.raises(ValueError, match="dropna must be unspecified"):
DataFrame(dtype=np.int64).stack(dropna=dropna, future_stack=future_stack)
else:
expected = int_frame
result = int_frame.copy().stack(
level=[], dropna=dropna, future_stack=future_stack
)
tm.assert_frame_equal(result, expected)

expected = DataFrame()
result = DataFrame().stack(level=[], dropna=dropna, future_stack=future_stack)
tm.assert_frame_equal(result, expected)


@pytest.mark.filterwarnings("ignore:The previous implementation of stack is deprecated")
@pytest.mark.parametrize("dropna", [True, False, lib.no_default])
@pytest.mark.parametrize("fill_value", [None, 0])
Expand Down

0 comments on commit e830603

Please sign in to comment.