Skip to content

Commit fc8605c

Browse files
natmokvalim-vinicius
authored and
im-vinicius
committed
DEPR: deprecate obj argument in GroupBy.get_group (pandas-dev#53571)
1 parent 4a70a1e commit fc8605c

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ Deprecations
289289
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
290290
- Deprecated falling back to filling when ``value`` is not specified in :meth:`DataFrame.replace` and :meth:`Series.replace` with non-dict-like ``to_replace`` (:issue:`33302`)
291291
- Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`)
292+
- Deprecated parameter ``obj`` in :meth:`GroupBy.get_group` (:issue:`53545`)
292293
- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`)
293294
- Deprecated the "method" and "limit" keywords on :meth:`Series.fillna`, :meth:`DataFrame.fillna`, :meth:`SeriesGroupBy.fillna`, :meth:`DataFrameGroupBy.fillna`, and :meth:`Resampler.fillna`, use ``obj.bfill()`` or ``obj.ffill()`` instead (:issue:`53394`)
294295
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)

pandas/core/groupby/groupby.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,11 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
925925
it is None, the object groupby was called on will
926926
be used.
927927
928+
.. deprecated:: 2.1.0
929+
The obj is deprecated and will be removed in a future version.
930+
Do ``df.iloc[gb.indices.get(name)]``
931+
instead of ``gb.get_group(name, obj=df)``.
932+
928933
Returns
929934
-------
930935
same type as obj
@@ -961,14 +966,21 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
961966
owl 1 2 3
962967
toucan 1 5 6
963968
"""
964-
if obj is None:
965-
obj = self._selected_obj
966-
967969
inds = self._get_index(name)
968970
if not len(inds):
969971
raise KeyError(name)
970972

971-
return obj._take_with_is_copy(inds, axis=self.axis)
973+
if obj is None:
974+
return self._selected_obj.iloc[inds]
975+
else:
976+
warnings.warn(
977+
"obj is deprecated and will be removed in a future version. "
978+
"Do ``df.iloc[gb.indices.get(name)]`` "
979+
"instead of ``gb.get_group(name, obj=df)``.",
980+
FutureWarning,
981+
stacklevel=find_stack_level(),
982+
)
983+
return obj._take_with_is_copy(inds, axis=self.axis)
972984

973985
@final
974986
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:

pandas/tests/groupby/test_groupby.py

+10
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,16 @@ def test_as_index_select_column():
695695
tm.assert_series_equal(result, expected)
696696

697697

698+
def test_obj_arg_get_group_deprecated():
699+
depr_msg = "obj is deprecated"
700+
701+
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
702+
expected = df.iloc[df.groupby("b").indices.get(4)]
703+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
704+
result = df.groupby("b").get_group(4, obj=df)
705+
tm.assert_frame_equal(result, expected)
706+
707+
698708
def test_groupby_as_index_select_column_sum_empty_df():
699709
# GH 35246
700710
df = DataFrame(columns=Index(["A", "B", "C"], name="alpha"))

0 commit comments

Comments
 (0)