Skip to content

DEPR: deprecate obj argument in GroupBy.get_group #53571

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Deprecations
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
- 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`)
- Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`)
- Deprecated parameter ``obj`` in :meth:`GroupBy.get_group` (:issue:`53545`)
- 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`)
- 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`)
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)
Expand Down
20 changes: 16 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,11 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
it is None, the object groupby was called on will
be used.

.. deprecated:: 2.1.0
The obj is deprecated and will be removed in a future version.
Do ``df.iloc[gb.indices.get(name)]``
instead of ``gb.get_group(name, obj=df)``.

Returns
-------
same type as obj
Expand Down Expand Up @@ -961,14 +966,21 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
owl 1 2 3
toucan 1 5 6
"""
if obj is None:
obj = self._selected_obj

inds = self._get_index(name)
if not len(inds):
raise KeyError(name)

return obj._take_with_is_copy(inds, axis=self.axis)
if obj is None:
return self._selected_obj.iloc[inds]
else:
warnings.warn(
"obj is deprecated and will be removed in a future version. "
"Do ``df.iloc[gb.indices.get(name)]`` "
"instead of ``gb.get_group(name, obj=df)``.",
FutureWarning,
stacklevel=find_stack_level(),
)
return obj._take_with_is_copy(inds, axis=self.axis)

@final
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,16 @@ def test_as_index_select_column():
tm.assert_series_equal(result, expected)


def test_obj_arg_get_group_deprecated():
depr_msg = "obj is deprecated"

df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
expected = df.iloc[df.groupby("b").indices.get(4)]
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
result = df.groupby("b").get_group(4, obj=df)
tm.assert_frame_equal(result, expected)


def test_groupby_as_index_select_column_sum_empty_df():
# GH 35246
df = DataFrame(columns=Index(["A", "B", "C"], name="alpha"))
Expand Down