Skip to content

DEPR: Previous implementation of DataFrame.stack #55448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -261,6 +261,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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my edification, when this deprecation is enforced will we need to change all/most of these unit tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - but all tests that use stack test with both future_stack=False and future_stack=True. So it will just be ripping out the future_stack=False case.

)
@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