Skip to content

DEPR: Categorical.to_list, NDFrame.align broadcast_axis #52130

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 2 commits into from
Mar 23, 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ Deprecations
- Deprecated the default of ``observed=False`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby`; this will default to ``True`` in a future version (:issue:`43999`)
- Deprecated :meth:`DataFrameGroupBy.dtypes`, check ``dtypes`` on the underlying object instead (:issue:`51045`)
- Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`)
- Deprecated :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`)
- Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`)
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
- Deprecated ``axis=1`` in :meth:`DataFrame.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`)
- Deprecated the ``axis`` keyword in :meth:`DataFrame.ewm`, :meth:`Series.ewm`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, :meth:`Series.expanding` (:issue:`51778`)
- Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`)
- Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`)
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)
-

Expand Down
9 changes: 9 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
cast,
overload,
)
import warnings

import numpy as np

Expand All @@ -25,6 +26,7 @@
)
from pandas._libs.arrays import NDArrayBacked
from pandas.compat.numpy import function as nv
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -551,6 +553,13 @@ def to_list(self):
"""
Alias for tolist.
"""
# GH#51254
warnings.warn(
"Categorical.to_list is deprecated and will be removed in a future "
"version. Use obj.tolist() instead",
FutureWarning,
stacklevel=find_stack_level(),
)
return self.tolist()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5033,7 +5033,7 @@ def align(
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
limit: int | None | lib.NoDefault = lib.no_default,
fill_axis: Axis | lib.NoDefault = lib.no_default,
broadcast_axis: Axis | None = None,
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
) -> tuple[Self, NDFrameT]:
return super().align(
other,
Expand Down
33 changes: 31 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9333,7 +9333,7 @@ def align(
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
limit: int | None | lib.NoDefault = lib.no_default,
fill_axis: Axis | lib.NoDefault = lib.no_default,
broadcast_axis: Axis | None = None,
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
) -> tuple[Self, NDFrameT]:
"""
Align two objects on their axes with the specified join method.
Expand Down Expand Up @@ -9382,6 +9382,8 @@ def align(
Broadcast values along this axis, if aligning two objects of
different dimensions.
.. deprecated:: 2.1
Returns
-------
tuple of ({klass}, type of other)
Expand Down Expand Up @@ -9474,6 +9476,27 @@ def align(
limit = None
method = clean_fill_method(method)

if broadcast_axis is not lib.no_default:
# GH#51856
msg = (
f"The 'broadcast_axis' keyword in {type(self).__name__}.align is "
"deprecated and will be removed in a future version."
)
if broadcast_axis is not None:
if self.ndim == 1 and other.ndim == 2:
msg += (
" Use left = DataFrame({col: left for col in right.columns}, "
"index=right.index) before calling `left.align(right)` instead."
)
elif self.ndim == 2 and other.ndim == 1:
msg += (
" Use right = DataFrame({col: right for col in left.columns}, "
"index=left.index) before calling `left.align(right)` instead"
)
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
else:
broadcast_axis = None

if broadcast_axis == 1 and self.ndim != other.ndim:
if isinstance(self, ABCSeries):
# this means other is a DataFrame, and we need to broadcast
Expand Down Expand Up @@ -9719,7 +9742,13 @@ def _where(
cond = common.apply_if_callable(cond, self)
if isinstance(cond, NDFrame):
# CoW: Make sure reference is not kept alive
cond = cond.align(self, join="right", broadcast_axis=1, copy=False)[0]
if cond.ndim == 1 and self.ndim == 2:
cond = cond._constructor_expanddim(
{i: cond for i in range(len(self.columns))},
copy=False,
)
cond.columns = self.columns
cond = cond.align(self, join="right", copy=False)[0]
else:
if not hasattr(cond, "shape"):
cond = np.asanyarray(cond)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4580,7 +4580,7 @@ def align(
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
limit: int | None | lib.NoDefault = lib.no_default,
fill_axis: Axis | lib.NoDefault = lib.no_default,
broadcast_axis: Axis | None = None,
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
) -> tuple[Self, NDFrameT]:
return super().align(
other,
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@


class TestCategoricalAPI:
def test_to_list_deprecated(self):
# GH#51254
cat1 = Categorical(list("acb"), ordered=False)
msg = "Categorical.to_list is deprecated and will be removed"
with tm.assert_produces_warning(FutureWarning, match=msg):
cat1.to_list()

def test_ordered_api(self):
# GH 9347
cat1 = Categorical(list("acb"), ordered=False)
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/frame/methods/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def test_align_frame_with_series(self, float_frame):
tm.assert_index_equal(right.index, float_frame.index)
assert isinstance(right, Series)

left, right = float_frame.align(s, broadcast_axis=1)
msg = "The 'broadcast_axis' keyword in DataFrame.align is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
left, right = float_frame.align(s, broadcast_axis=1)
tm.assert_index_equal(left.index, float_frame.index)
expected = {c: s for c in float_frame.columns}
expected = DataFrame(
Expand Down