Skip to content

Commit 922103a

Browse files
quangngdShashwatAgrawal20
authored andcommitted
BUG: stack with empty level list (pandas-dev#60826)
* return early if set_levels is empty * add test * add whatsnew * check empty before make set
1 parent 7e93d84 commit 922103a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ Reshaping
766766
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
767767
- Bug in :meth:`DataFrame.merge` when merging two :class:`DataFrame` on ``intc`` or ``uintc`` types on Windows (:issue:`60091`, :issue:`58713`)
768768
- Bug in :meth:`DataFrame.pivot_table` incorrectly subaggregating results when called without an ``index`` argument (:issue:`58722`)
769+
- Bug in :meth:`DataFrame.stack` with the new implementation where ``ValueError`` is raised when ``level=[]`` (:issue:`60740`)
769770
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
770771

771772
Sparse

pandas/core/reshape/reshape.py

+2
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,8 @@ def _reorder_for_extension_array_stack(
929929
def stack_v3(frame: DataFrame, level: list[int]) -> Series | DataFrame:
930930
if frame.columns.nunique() != len(frame.columns):
931931
raise ValueError("Columns with duplicate values are not supported in stack")
932+
if not len(level):
933+
return frame
932934
set_levels = set(level)
933935
stack_cols = frame.columns._drop_level_numbers(
934936
[k for k in range(frame.columns.nlevels - 1, -1, -1) if k not in set_levels]

pandas/tests/frame/test_stack_unstack.py

+19
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,25 @@ def test_stack_empty_frame(dropna, future_stack):
14521452
tm.assert_series_equal(result, expected)
14531453

14541454

1455+
@pytest.mark.filterwarnings("ignore:The previous implementation of stack is deprecated")
1456+
@pytest.mark.parametrize("dropna", [True, False, lib.no_default])
1457+
def test_stack_empty_level(dropna, future_stack, int_frame):
1458+
# GH 60740
1459+
if future_stack and dropna is not lib.no_default:
1460+
with pytest.raises(ValueError, match="dropna must be unspecified"):
1461+
DataFrame(dtype=np.int64).stack(dropna=dropna, future_stack=future_stack)
1462+
else:
1463+
expected = int_frame
1464+
result = int_frame.copy().stack(
1465+
level=[], dropna=dropna, future_stack=future_stack
1466+
)
1467+
tm.assert_frame_equal(result, expected)
1468+
1469+
expected = DataFrame()
1470+
result = DataFrame().stack(level=[], dropna=dropna, future_stack=future_stack)
1471+
tm.assert_frame_equal(result, expected)
1472+
1473+
14551474
@pytest.mark.filterwarnings("ignore:The previous implementation of stack is deprecated")
14561475
@pytest.mark.parametrize("dropna", [True, False, lib.no_default])
14571476
@pytest.mark.parametrize("fill_value", [None, 0])

0 commit comments

Comments
 (0)