Skip to content

Commit aff52bf

Browse files
authored
DEPR: Categorical.to_list, NDFrame.align broadcast_axis (#52130)
* DEPR: Categorical.to_list, NDFrame.align broadcast_axis * typo fixup
1 parent 3083ae9 commit aff52bf

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ Deprecations
104104
- 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`)
105105
- Deprecated :meth:`DataFrameGroupBy.dtypes`, check ``dtypes`` on the underlying object instead (:issue:`51045`)
106106
- Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`)
107+
- Deprecated :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`)
107108
- Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`)
108109
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
109110
- Deprecated ``axis=1`` in :meth:`DataFrame.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`)
110111
- 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`)
111112
- 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`)
113+
- 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`)
112114
- 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`)
113115
-
114116

pandas/core/arrays/categorical.py

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
cast,
1414
overload,
1515
)
16+
import warnings
1617

1718
import numpy as np
1819

@@ -25,6 +26,7 @@
2526
)
2627
from pandas._libs.arrays import NDArrayBacked
2728
from pandas.compat.numpy import function as nv
29+
from pandas.util._exceptions import find_stack_level
2830
from pandas.util._validators import validate_bool_kwarg
2931

3032
from pandas.core.dtypes.cast import (
@@ -551,6 +553,13 @@ def to_list(self):
551553
"""
552554
Alias for tolist.
553555
"""
556+
# GH#51254
557+
warnings.warn(
558+
"Categorical.to_list is deprecated and will be removed in a future "
559+
"version. Use obj.tolist() instead",
560+
FutureWarning,
561+
stacklevel=find_stack_level(),
562+
)
554563
return self.tolist()
555564

556565
@classmethod

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5033,7 +5033,7 @@ def align(
50335033
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
50345034
limit: int | None | lib.NoDefault = lib.no_default,
50355035
fill_axis: Axis | lib.NoDefault = lib.no_default,
5036-
broadcast_axis: Axis | None = None,
5036+
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
50375037
) -> tuple[Self, NDFrameT]:
50385038
return super().align(
50395039
other,

pandas/core/generic.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -9333,7 +9333,7 @@ def align(
93339333
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
93349334
limit: int | None | lib.NoDefault = lib.no_default,
93359335
fill_axis: Axis | lib.NoDefault = lib.no_default,
9336-
broadcast_axis: Axis | None = None,
9336+
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
93379337
) -> tuple[Self, NDFrameT]:
93389338
"""
93399339
Align two objects on their axes with the specified join method.
@@ -9382,6 +9382,8 @@ def align(
93829382
Broadcast values along this axis, if aligning two objects of
93839383
different dimensions.
93849384
9385+
.. deprecated:: 2.1
9386+
93859387
Returns
93869388
-------
93879389
tuple of ({klass}, type of other)
@@ -9474,6 +9476,27 @@ def align(
94749476
limit = None
94759477
method = clean_fill_method(method)
94769478

9479+
if broadcast_axis is not lib.no_default:
9480+
# GH#51856
9481+
msg = (
9482+
f"The 'broadcast_axis' keyword in {type(self).__name__}.align is "
9483+
"deprecated and will be removed in a future version."
9484+
)
9485+
if broadcast_axis is not None:
9486+
if self.ndim == 1 and other.ndim == 2:
9487+
msg += (
9488+
" Use left = DataFrame({col: left for col in right.columns}, "
9489+
"index=right.index) before calling `left.align(right)` instead."
9490+
)
9491+
elif self.ndim == 2 and other.ndim == 1:
9492+
msg += (
9493+
" Use right = DataFrame({col: right for col in left.columns}, "
9494+
"index=left.index) before calling `left.align(right)` instead"
9495+
)
9496+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
9497+
else:
9498+
broadcast_axis = None
9499+
94779500
if broadcast_axis == 1 and self.ndim != other.ndim:
94789501
if isinstance(self, ABCSeries):
94799502
# this means other is a DataFrame, and we need to broadcast
@@ -9719,7 +9742,13 @@ def _where(
97199742
cond = common.apply_if_callable(cond, self)
97209743
if isinstance(cond, NDFrame):
97219744
# CoW: Make sure reference is not kept alive
9722-
cond = cond.align(self, join="right", broadcast_axis=1, copy=False)[0]
9745+
if cond.ndim == 1 and self.ndim == 2:
9746+
cond = cond._constructor_expanddim(
9747+
{i: cond for i in range(len(self.columns))},
9748+
copy=False,
9749+
)
9750+
cond.columns = self.columns
9751+
cond = cond.align(self, join="right", copy=False)[0]
97239752
else:
97249753
if not hasattr(cond, "shape"):
97259754
cond = np.asanyarray(cond)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4580,7 +4580,7 @@ def align(
45804580
method: FillnaOptions | None | lib.NoDefault = lib.no_default,
45814581
limit: int | None | lib.NoDefault = lib.no_default,
45824582
fill_axis: Axis | lib.NoDefault = lib.no_default,
4583-
broadcast_axis: Axis | None = None,
4583+
broadcast_axis: Axis | None | lib.NoDefault = lib.no_default,
45844584
) -> tuple[Self, NDFrameT]:
45854585
return super().align(
45864586
other,

pandas/tests/arrays/categorical/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919

2020
class TestCategoricalAPI:
21+
def test_to_list_deprecated(self):
22+
# GH#51254
23+
cat1 = Categorical(list("acb"), ordered=False)
24+
msg = "Categorical.to_list is deprecated and will be removed"
25+
with tm.assert_produces_warning(FutureWarning, match=msg):
26+
cat1.to_list()
27+
2128
def test_ordered_api(self):
2229
# GH 9347
2330
cat1 = Categorical(list("acb"), ordered=False)

pandas/tests/frame/methods/test_align.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def test_align_frame_with_series(self, float_frame):
126126
tm.assert_index_equal(right.index, float_frame.index)
127127
assert isinstance(right, Series)
128128

129-
left, right = float_frame.align(s, broadcast_axis=1)
129+
msg = "The 'broadcast_axis' keyword in DataFrame.align is deprecated"
130+
with tm.assert_produces_warning(FutureWarning, match=msg):
131+
left, right = float_frame.align(s, broadcast_axis=1)
130132
tm.assert_index_equal(left.index, float_frame.index)
131133
expected = {c: s for c in float_frame.columns}
132134
expected = DataFrame(

0 commit comments

Comments
 (0)