diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 19db7dcb4b83e..41d519e0765dc 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2d181e826c2a9..01170320e5e31 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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, @@ -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) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5c7d0eae24cee..8aa8f8bb60654 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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. diff --git a/pandas/core/series.py b/pandas/core/series.py index e107b66d33b1c..bc13d5376ec96 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 @@ -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) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index c88d16e34eab8..a0d059fc8f83b 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -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( @@ -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)