Skip to content
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ Deprecations
- :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`)
- :meth:`Index.get_value` is deprecated and will be removed in a future version (:issue:`19728`)
- :meth:`DateOffset.__call__` is deprecated and will be removed in a future version, use ``offset + other`` instead (:issue:`34171`)
- The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`)

.. ---------------------------------------------------------------------------

Expand Down
15 changes: 14 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from pandas._config import get_option

from pandas._libs import algos as libalgos, lib, properties
from pandas._libs.lib import no_default
from pandas._typing import (
ArrayLike,
Axes,
Expand Down Expand Up @@ -6253,12 +6254,24 @@ def groupby(
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
squeeze: bool = no_default,
observed: bool = False,
dropna: bool = True,
) -> "DataFrameGroupBy":
from pandas.core.groupby.generic import DataFrameGroupBy

if squeeze is not no_default:
warnings.warn(
(
"The `squeeze` parameter is deprecated and "
"will be removed in a future version."
),
FutureWarning,
stacklevel=2,
)
else:
squeeze = False

if level is None and by is None:
raise TypeError("You have to supply one of 'by' and 'level'")
axis = self._get_axis_number(axis)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7472,6 +7472,9 @@ def clip(
squeeze : bool, default False
Reduce the dimensionality of the return type if possible,
otherwise return a consistent type.

.. deprecated:: 1.1.0

observed : bool, default False
This only applies if any of the groupers are Categoricals.
If True: only show observed values for categorical groupers.
Expand Down
15 changes: 14 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pandas._config import get_option

from pandas._libs import lib, properties, reshape, tslibs
from pandas._libs.lib import no_default
from pandas._typing import ArrayLike, Axis, DtypeObj, IndexKeyFunc, Label, ValueKeyFunc
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, doc
Expand Down Expand Up @@ -1642,12 +1643,24 @@ def groupby(
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
squeeze: bool = no_default,
observed: bool = False,
dropna: bool = True,
) -> "SeriesGroupBy":
from pandas.core.groupby.generic import SeriesGroupBy

if squeeze is not no_default:
warnings.warn(
(
"The `squeeze` parameter is deprecated and "
"will be removed in a future version."
),
FutureWarning,
stacklevel=2,
)
else:
squeeze = False

if level is None and by is None:
raise TypeError("You have to supply one of 'by' and 'level'")
axis = self._get_axis_number(axis)
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def test_groupby_return_type():
def func(dataf):
return dataf["val2"] - dataf["val2"].mean()

result = df1.groupby("val1", squeeze=True).apply(func)
with tm.assert_produces_warning(FutureWarning):
result = df1.groupby("val1", squeeze=True).apply(func)
assert isinstance(result, Series)

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

result = df2.groupby("val1", squeeze=True).apply(func)
with tm.assert_produces_warning(FutureWarning):
result = df2.groupby("val1", squeeze=True).apply(func)
assert isinstance(result, Series)

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


Expand Down