Skip to content

Commit a83f6aa

Browse files
authored
DEPR: Previous implementation of DataFrame.stack (#55448)
* DEPR: Previous implementation of DataFrame.stack * Remove DataFrame.stack docs on dropping missing values
1 parent 8738635 commit a83f6aa

File tree

5 files changed

+126
-28
lines changed

5 files changed

+126
-28
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Other Deprecations
265265
- Deprecated the ``fastpath`` keyword in the :class:`Series` constructor (:issue:`20110`)
266266
- Deprecated the extension test classes ``BaseNoReduceTests``, ``BaseBooleanReduceTests``, and ``BaseNumericReduceTests``, use ``BaseReduceTests`` instead (:issue:`54663`)
267267
- Deprecated the option ``mode.data_manager`` and the ``ArrayManager``; only the ``BlockManager`` will be available in future versions (:issue:`55043`)
268+
- Deprecated the previous implementation of :class:`DataFrame.stack`; specify ``future_stack=True`` to adopt the future version (:issue:`53515`)
268269
- Deprecating downcasting the results of :meth:`DataFrame.fillna`, :meth:`Series.fillna`, :meth:`DataFrame.ffill`, :meth:`Series.ffill`, :meth:`DataFrame.bfill`, :meth:`Series.bfill` in object-dtype cases. To opt in to the future version, use ``pd.set_option("future.no_silent_downcasting", True)`` (:issue:`54261`)
269270
-
270271

pandas/core/frame.py

+14-27
Original file line numberDiff line numberDiff line change
@@ -9499,40 +9499,27 @@ def stack(
94999499
dog weight kg 3.0
95009500
height m 4.0
95019501
dtype: float64
9502-
9503-
**Dropping missing values**
9504-
9505-
>>> df_multi_level_cols3 = pd.DataFrame([[None, 1.0], [2.0, 3.0]],
9506-
... index=['cat', 'dog'],
9507-
... columns=multicol2)
9508-
9509-
Note that rows where all values are missing are dropped by
9510-
default but this behaviour can be controlled via the dropna
9511-
keyword parameter:
9512-
9513-
>>> df_multi_level_cols3
9514-
weight height
9515-
kg m
9516-
cat NaN 1.0
9517-
dog 2.0 3.0
9518-
>>> df_multi_level_cols3.stack(dropna=False)
9519-
weight height
9520-
cat kg NaN NaN
9521-
m NaN 1.0
9522-
dog kg 2.0 NaN
9523-
m NaN 3.0
9524-
>>> df_multi_level_cols3.stack(dropna=True)
9525-
weight height
9526-
cat m NaN 1.0
9527-
dog kg 2.0 NaN
9528-
m NaN 3.0
95299502
"""
95309503
if not future_stack:
95319504
from pandas.core.reshape.reshape import (
95329505
stack,
95339506
stack_multiple,
95349507
)
95359508

9509+
if (
9510+
dropna is not lib.no_default
9511+
or sort is not lib.no_default
9512+
or self.columns.nlevels > 1
9513+
):
9514+
warnings.warn(
9515+
"The previous implementation of stack is deprecated and will be "
9516+
"removed in a future version of pandas. See the What's New notes "
9517+
"for pandas 2.1.0 for details. Specify future_stack=True to adopt "
9518+
"the new implementation and silence this warning.",
9519+
FutureWarning,
9520+
stacklevel=find_stack_level(),
9521+
)
9522+
95369523
if dropna is lib.no_default:
95379524
dropna = True
95389525
if sort is lib.no_default:

pandas/tests/extension/base/reshaping.py

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ def test_merge_on_extension_array_duplicates(self, data):
249249
)
250250
tm.assert_frame_equal(result, expected)
251251

252+
@pytest.mark.filterwarnings(
253+
"ignore:The previous implementation of stack is deprecated"
254+
)
252255
@pytest.mark.parametrize(
253256
"columns",
254257
[

pandas/tests/extension/test_sparse.py

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def test_concat_mixed_dtypes(self, data):
133133
)
134134
tm.assert_frame_equal(result, expected)
135135

136+
@pytest.mark.filterwarnings(
137+
"ignore:The previous implementation of stack is deprecated"
138+
)
136139
@pytest.mark.parametrize(
137140
"columns",
138141
[

0 commit comments

Comments
 (0)