Skip to content
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

32380 deprecate squeeze in groupby #33218

Merged
merged 12 commits into from
May 22, 2020
15 changes: 14 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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 Axes, Axis, Dtype, FilePathOrBuffer, Label, Level, Renamer
from pandas.compat import PY37
from pandas.compat._optional import import_optional_dependency
Expand Down Expand Up @@ -5813,11 +5814,23 @@ def groupby(
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
squeeze: bool = no_default,
observed: bool = False,
) -> "DataFrameGroupBy":
from pandas.core.groupby.generic import DataFrameGroupBy

if squeeze is not no_default:
warnings.warn(
(
"The `squeeze` parameter in pd.groupby is deprecated and "
phofl marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -7419,6 +7419,9 @@ def clip(
squeeze : bool, default False
Reduce the dimensionality of the return type if possible,
otherwise return a consistent type.

deprecated:: 2.0
phofl marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -22,6 +22,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 Axis, DtypeObj, Label
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, doc
Expand Down Expand Up @@ -1646,11 +1647,23 @@ def groupby(
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
squeeze: bool = no_default,
observed: bool = False,
) -> "SeriesGroupBy":
from pandas.core.groupby.generic import SeriesGroupBy

if squeeze is not no_default:
warnings.warn(
(
"The `squeeze` parameter in pd.groupby is deprecated and "
phofl marked this conversation as resolved.
Show resolved Hide resolved
"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