Skip to content

Commit 7c46c68

Browse files
authored
32380 deprecate squeeze in groupby (#33218)
1 parent b3ab167 commit 7c46c68

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ Deprecations
590590
- :func:`pandas.api.types.is_categorical` is deprecated and will be removed in a future version; use `:func:pandas.api.types.is_categorical_dtype` instead (:issue:`33385`)
591591
- :meth:`Index.get_value` is deprecated and will be removed in a future version (:issue:`19728`)
592592
- :meth:`DateOffset.__call__` is deprecated and will be removed in a future version, use ``offset + other`` instead (:issue:`34171`)
593+
- The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`)
593594

594595
.. ---------------------------------------------------------------------------
595596

pandas/core/frame.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pandas._config import get_option
4343

4444
from pandas._libs import algos as libalgos, lib, properties
45+
from pandas._libs.lib import no_default
4546
from pandas._typing import (
4647
ArrayLike,
4748
Axes,
@@ -6253,12 +6254,24 @@ def groupby(
62536254
as_index: bool = True,
62546255
sort: bool = True,
62556256
group_keys: bool = True,
6256-
squeeze: bool = False,
6257+
squeeze: bool = no_default,
62576258
observed: bool = False,
62586259
dropna: bool = True,
62596260
) -> "DataFrameGroupBy":
62606261
from pandas.core.groupby.generic import DataFrameGroupBy
62616262

6263+
if squeeze is not no_default:
6264+
warnings.warn(
6265+
(
6266+
"The `squeeze` parameter is deprecated and "
6267+
"will be removed in a future version."
6268+
),
6269+
FutureWarning,
6270+
stacklevel=2,
6271+
)
6272+
else:
6273+
squeeze = False
6274+
62626275
if level is None and by is None:
62636276
raise TypeError("You have to supply one of 'by' and 'level'")
62646277
axis = self._get_axis_number(axis)

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -7472,6 +7472,9 @@ def clip(
74727472
squeeze : bool, default False
74737473
Reduce the dimensionality of the return type if possible,
74747474
otherwise return a consistent type.
7475+
7476+
.. deprecated:: 1.1.0
7477+
74757478
observed : bool, default False
74767479
This only applies if any of the groupers are Categoricals.
74777480
If True: only show observed values for categorical groupers.

pandas/core/series.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pandas._config import get_option
2424

2525
from pandas._libs import lib, properties, reshape, tslibs
26+
from pandas._libs.lib import no_default
2627
from pandas._typing import ArrayLike, Axis, DtypeObj, IndexKeyFunc, Label, ValueKeyFunc
2728
from pandas.compat.numpy import function as nv
2829
from pandas.util._decorators import Appender, Substitution, doc
@@ -1642,12 +1643,24 @@ def groupby(
16421643
as_index: bool = True,
16431644
sort: bool = True,
16441645
group_keys: bool = True,
1645-
squeeze: bool = False,
1646+
squeeze: bool = no_default,
16461647
observed: bool = False,
16471648
dropna: bool = True,
16481649
) -> "SeriesGroupBy":
16491650
from pandas.core.groupby.generic import SeriesGroupBy
16501651

1652+
if squeeze is not no_default:
1653+
warnings.warn(
1654+
(
1655+
"The `squeeze` parameter is deprecated and "
1656+
"will be removed in a future version."
1657+
),
1658+
FutureWarning,
1659+
stacklevel=2,
1660+
)
1661+
else:
1662+
squeeze = False
1663+
16511664
if level is None and by is None:
16521665
raise TypeError("You have to supply one of 'by' and 'level'")
16531666
axis = self._get_axis_number(axis)

pandas/tests/groupby/test_groupby.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def test_groupby_return_type():
109109
def func(dataf):
110110
return dataf["val2"] - dataf["val2"].mean()
111111

112-
result = df1.groupby("val1", squeeze=True).apply(func)
112+
with tm.assert_produces_warning(FutureWarning):
113+
result = df1.groupby("val1", squeeze=True).apply(func)
113114
assert isinstance(result, Series)
114115

115116
df2 = DataFrame(
@@ -124,12 +125,14 @@ def func(dataf):
124125
def func(dataf):
125126
return dataf["val2"] - dataf["val2"].mean()
126127

127-
result = df2.groupby("val1", squeeze=True).apply(func)
128+
with tm.assert_produces_warning(FutureWarning):
129+
result = df2.groupby("val1", squeeze=True).apply(func)
128130
assert isinstance(result, Series)
129131

130132
# GH3596, return a consistent type (regression in 0.11 from 0.10.1)
131133
df = DataFrame([[1, 1], [1, 1]], columns=["X", "Y"])
132-
result = df.groupby("X", squeeze=False).count()
134+
with tm.assert_produces_warning(FutureWarning):
135+
result = df.groupby("X", squeeze=False).count()
133136
assert isinstance(result, DataFrame)
134137

135138

0 commit comments

Comments
 (0)