Skip to content

Commit fe93a83

Browse files
authored
DEP: Enforce deprecation of squeeze argument in groupby (#49082)
1 parent 2410fca commit fe93a83

File tree

9 files changed

+8
-123
lines changed

9 files changed

+8
-123
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Deprecations
144144

145145
Removal of prior version deprecations/changes
146146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
- Remove argument ``squeeze`` from :meth:`DataFrame.groupby` and :meth:`Series.groupby` (:issue:`32380`)
147148
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
148149
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
149150
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)

pandas/core/frame.py

-14
Original file line numberDiff line numberDiff line change
@@ -8384,24 +8384,11 @@ def groupby(
83848384
as_index: bool = True,
83858385
sort: bool = True,
83868386
group_keys: bool | lib.NoDefault = no_default,
8387-
squeeze: bool | lib.NoDefault = no_default,
83888387
observed: bool = False,
83898388
dropna: bool = True,
83908389
) -> DataFrameGroupBy:
83918390
from pandas.core.groupby.generic import DataFrameGroupBy
83928391

8393-
if squeeze is not no_default:
8394-
warnings.warn(
8395-
(
8396-
"The `squeeze` parameter is deprecated and "
8397-
"will be removed in a future version."
8398-
),
8399-
FutureWarning,
8400-
stacklevel=find_stack_level(),
8401-
)
8402-
else:
8403-
squeeze = False
8404-
84058392
if level is None and by is None:
84068393
raise TypeError("You have to supply one of 'by' and 'level'")
84078394
axis = self._get_axis_number(axis)
@@ -8414,7 +8401,6 @@ def groupby(
84148401
as_index=as_index,
84158402
sort=sort,
84168403
group_keys=group_keys,
8417-
squeeze=squeeze,
84188404
observed=observed,
84198405
dropna=dropna,
84208406
)

pandas/core/groupby/generic.py

-29
Original file line numberDiff line numberDiff line change
@@ -1317,33 +1317,6 @@ def _wrap_applied_output_series(
13171317

13181318
all_indexed_same = all_indexes_same(x.index for x in values)
13191319

1320-
# GH3596
1321-
# provide a reduction (Frame -> Series) if groups are
1322-
# unique
1323-
if self.squeeze:
1324-
applied_index = self._selected_obj._get_axis(self.axis)
1325-
singular_series = len(values) == 1 and applied_index.nlevels == 1
1326-
1327-
if singular_series:
1328-
# GH2893
1329-
# we have series in the values array, we want to
1330-
# produce a series:
1331-
# if any of the sub-series are not indexed the same
1332-
# OR we don't have a multi-index and we have only a
1333-
# single values
1334-
return self._concat_objects(
1335-
values,
1336-
not_indexed_same=not_indexed_same,
1337-
override_group_keys=override_group_keys,
1338-
)
1339-
1340-
# still a series
1341-
# path added as of GH 5545
1342-
elif all_indexed_same:
1343-
from pandas.core.reshape.concat import concat
1344-
1345-
return concat(values)
1346-
13471320
if not all_indexed_same:
13481321
# GH 8467
13491322
return self._concat_objects(
@@ -1673,7 +1646,6 @@ def _gotitem(self, key, ndim: int, subset=None):
16731646
as_index=self.as_index,
16741647
sort=self.sort,
16751648
group_keys=self.group_keys,
1676-
squeeze=self.squeeze,
16771649
observed=self.observed,
16781650
mutated=self.mutated,
16791651
dropna=self.dropna,
@@ -1688,7 +1660,6 @@ def _gotitem(self, key, ndim: int, subset=None):
16881660
selection=key,
16891661
sort=self.sort,
16901662
group_keys=self.group_keys,
1691-
squeeze=self.squeeze,
16921663
observed=self.observed,
16931664
dropna=self.dropna,
16941665
)

pandas/core/groupby/groupby.py

-5
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
645645
"obj",
646646
"observed",
647647
"sort",
648-
"squeeze",
649648
}
650649

651650
axis: AxisInt
@@ -929,7 +928,6 @@ def __init__(
929928
as_index: bool = True,
930929
sort: bool = True,
931930
group_keys: bool | lib.NoDefault = True,
932-
squeeze: bool = False,
933931
observed: bool = False,
934932
mutated: bool = False,
935933
dropna: bool = True,
@@ -951,7 +949,6 @@ def __init__(
951949
self.keys = keys
952950
self.sort = sort
953951
self.group_keys = group_keys
954-
self.squeeze = squeeze
955952
self.observed = observed
956953
self.mutated = mutated
957954
self.dropna = dropna
@@ -4328,7 +4325,6 @@ def get_groupby(
43284325
as_index: bool = True,
43294326
sort: bool = True,
43304327
group_keys: bool | lib.NoDefault = True,
4331-
squeeze: bool = False,
43324328
observed: bool = False,
43334329
mutated: bool = False,
43344330
dropna: bool = True,
@@ -4357,7 +4353,6 @@ def get_groupby(
43574353
as_index=as_index,
43584354
sort=sort,
43594355
group_keys=group_keys,
4360-
squeeze=squeeze,
43614356
observed=observed,
43624357
mutated=mutated,
43634358
dropna=dropna,

pandas/core/resample.py

-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ def __init__(
164164
# [int, Literal['index', 'columns', 'rows']]", variable has type "int")
165165
self.axis = axis # type: ignore[assignment]
166166
self.kind = kind
167-
self.squeeze = False
168167
self.group_keys = group_keys
169168
self.as_index = True
170169

pandas/core/series.py

-14
Original file line numberDiff line numberDiff line change
@@ -2026,24 +2026,11 @@ def groupby(
20262026
as_index: bool = True,
20272027
sort: bool = True,
20282028
group_keys: bool | lib.NoDefault = no_default,
2029-
squeeze: bool | lib.NoDefault = no_default,
20302029
observed: bool = False,
20312030
dropna: bool = True,
20322031
) -> SeriesGroupBy:
20332032
from pandas.core.groupby.generic import SeriesGroupBy
20342033

2035-
if squeeze is not no_default:
2036-
warnings.warn(
2037-
(
2038-
"The `squeeze` parameter is deprecated and "
2039-
"will be removed in a future version."
2040-
),
2041-
FutureWarning,
2042-
stacklevel=find_stack_level(),
2043-
)
2044-
else:
2045-
squeeze = False
2046-
20472034
if level is None and by is None:
20482035
raise TypeError("You have to supply one of 'by' and 'level'")
20492036
axis = self._get_axis_number(axis)
@@ -2056,7 +2043,6 @@ def groupby(
20562043
as_index=as_index,
20572044
sort=sort,
20582045
group_keys=group_keys,
2059-
squeeze=squeeze,
20602046
observed=observed,
20612047
dropna=dropna,
20622048
)

pandas/core/shared_docs.py

-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@
133133
result from ``apply`` is a like-indexed Series or DataFrame.
134134
Specify ``group_keys`` explicitly to include the group keys or
135135
not.
136-
squeeze : bool, default False
137-
Reduce the dimensionality of the return type if possible,
138-
otherwise return a consistent type.
139-
140-
.. deprecated:: 1.1.0
141136
142137
observed : bool, default False
143138
This only applies if any of the groupers are Categoricals.

pandas/tests/groupby/test_groupby.py

-45
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,6 @@ def max_value(group):
109109
tm.assert_series_equal(result, expected)
110110

111111

112-
def test_groupby_return_type():
113-
114-
# GH2893, return a reduced type
115-
116-
def func(dataf):
117-
return dataf["val2"] - dataf["val2"].mean()
118-
119-
df1 = DataFrame(
120-
[
121-
{"val1": 1, "val2": 20},
122-
{"val1": 1, "val2": 19},
123-
{"val1": 2, "val2": 27},
124-
{"val1": 2, "val2": 12},
125-
]
126-
)
127-
128-
with tm.assert_produces_warning(FutureWarning):
129-
result = df1.groupby("val1", squeeze=True).apply(func)
130-
assert isinstance(result, Series)
131-
132-
df2 = DataFrame(
133-
[
134-
{"val1": 1, "val2": 20},
135-
{"val1": 1, "val2": 19},
136-
{"val1": 1, "val2": 27},
137-
{"val1": 1, "val2": 12},
138-
]
139-
)
140-
141-
with tm.assert_produces_warning(FutureWarning):
142-
result = df2.groupby("val1", squeeze=True).apply(func)
143-
assert isinstance(result, Series)
144-
145-
# GH3596, return a consistent type (regression in 0.11 from 0.10.1)
146-
df = DataFrame([[1, 1], [1, 1]], columns=["X", "Y"])
147-
with tm.assert_produces_warning(FutureWarning):
148-
result = df.groupby("X", squeeze=False).count()
149-
assert isinstance(result, DataFrame)
150-
151-
152112
def test_inconsistent_return_type():
153113
# GH5592
154114
# inconsistent return type
@@ -2498,7 +2458,6 @@ def test_group_on_two_row_multiindex_returns_one_tuple_key():
24982458
(DataFrame, "as_index", False),
24992459
(DataFrame, "sort", False),
25002460
(DataFrame, "group_keys", False),
2501-
(DataFrame, "squeeze", True),
25022461
(DataFrame, "observed", True),
25032462
(DataFrame, "dropna", False),
25042463
pytest.param(
@@ -2513,14 +2472,10 @@ def test_group_on_two_row_multiindex_returns_one_tuple_key():
25132472
(Series, "as_index", False),
25142473
(Series, "sort", False),
25152474
(Series, "group_keys", False),
2516-
(Series, "squeeze", True),
25172475
(Series, "observed", True),
25182476
(Series, "dropna", False),
25192477
],
25202478
)
2521-
@pytest.mark.filterwarnings(
2522-
"ignore:The `squeeze` parameter is deprecated:FutureWarning"
2523-
)
25242479
def test_subsetting_columns_keeps_attrs(klass, attr, value):
25252480
# GH 9959 - When subsetting columns, don't drop attributes
25262481
df = DataFrame({"a": [1], "b": [2], "c": [3]})

pandas/tests/groupby/test_timegrouper.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,7 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
887887
# We need to create a GroupBy object with only one non-NaT group,
888888
# so use a huge freq so that all non-NaT dates will be grouped together
889889
tdg = Grouper(key="Date", freq="100Y")
890-
891-
with tm.assert_produces_warning(FutureWarning, match="`squeeze` parameter"):
892-
gb = df.groupby(tdg, squeeze=True)
890+
gb = df.groupby(tdg)
893891

894892
# check that we will go through the singular_series path
895893
# in _wrap_applied_output_series
@@ -899,13 +897,12 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
899897
# function that returns a Series
900898
res = gb.apply(lambda x: x["Quantity"] * 2)
901899

902-
key = Timestamp("2013-12-31")
903-
ordering = df["Date"].sort_values().dropna().index
904-
mi = MultiIndex.from_product([[key], ordering], names=["Date", None])
905-
906-
ex_values = df["Quantity"].take(ordering).values * 2
907-
expected = Series(ex_values, index=mi, name="Quantity")
908-
tm.assert_series_equal(res, expected)
900+
expected = DataFrame(
901+
[[36, 6, 6, 10, 2]],
902+
index=Index([Timestamp("2013-12-31")], name="Date"),
903+
columns=Index([0, 1, 5, 2, 3], name="Quantity"),
904+
)
905+
tm.assert_frame_equal(res, expected)
909906

910907
@td.skip_if_no("numba")
911908
def test_groupby_agg_numba_timegrouper_with_nat(

0 commit comments

Comments
 (0)