Skip to content

Commit

Permalink
DEPR: Previous implementation of DataFrame.stack (#55448)
Browse files Browse the repository at this point in the history
* DEPR: Previous implementation of DataFrame.stack

* Remove DataFrame.stack docs on dropping missing values
  • Loading branch information
rhshadrach authored Oct 17, 2023
1 parent 8738635 commit a83f6aa
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 28 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Other Deprecations
- Deprecated the ``fastpath`` keyword in the :class:`Series` constructor (:issue:`20110`)
- Deprecated the extension test classes ``BaseNoReduceTests``, ``BaseBooleanReduceTests``, and ``BaseNumericReduceTests``, use ``BaseReduceTests`` instead (:issue:`54663`)
- Deprecated the option ``mode.data_manager`` and the ``ArrayManager``; only the ``BlockManager`` will be available in future versions (:issue:`55043`)
- Deprecated the previous implementation of :class:`DataFrame.stack`; specify ``future_stack=True`` to adopt the future version (:issue:`53515`)
- 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`)
-

Expand Down
41 changes: 14 additions & 27 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9499,40 +9499,27 @@ def stack(
dog weight kg 3.0
height m 4.0
dtype: float64
**Dropping missing values**
>>> df_multi_level_cols3 = pd.DataFrame([[None, 1.0], [2.0, 3.0]],
... index=['cat', 'dog'],
... columns=multicol2)
Note that rows where all values are missing are dropped by
default but this behaviour can be controlled via the dropna
keyword parameter:
>>> df_multi_level_cols3
weight height
kg m
cat NaN 1.0
dog 2.0 3.0
>>> df_multi_level_cols3.stack(dropna=False)
weight height
cat kg NaN NaN
m NaN 1.0
dog kg 2.0 NaN
m NaN 3.0
>>> df_multi_level_cols3.stack(dropna=True)
weight height
cat m NaN 1.0
dog kg 2.0 NaN
m NaN 3.0
"""
if not future_stack:
from pandas.core.reshape.reshape import (
stack,
stack_multiple,
)

if (
dropna is not lib.no_default
or sort is not lib.no_default
or self.columns.nlevels > 1
):
warnings.warn(
"The previous implementation of stack is deprecated and will be "
"removed in a future version of pandas. See the What's New notes "
"for pandas 2.1.0 for details. Specify future_stack=True to adopt "
"the new implementation and silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
)

if dropna is lib.no_default:
dropna = True
if sort is lib.no_default:
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ def test_merge_on_extension_array_duplicates(self, data):
)
tm.assert_frame_equal(result, expected)

@pytest.mark.filterwarnings(
"ignore:The previous implementation of stack is deprecated"
)
@pytest.mark.parametrize(
"columns",
[
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def test_concat_mixed_dtypes(self, data):
)
tm.assert_frame_equal(result, expected)

@pytest.mark.filterwarnings(
"ignore:The previous implementation of stack is deprecated"
)
@pytest.mark.parametrize(
"columns",
[
Expand Down
Loading

0 comments on commit a83f6aa

Please sign in to comment.