Skip to content

BUG: Bug fix for GH48567 #48772

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 3 commits into from
Sep 30, 2022
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/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
- Bug in :meth:`DataFrameGroupBy.sample` raises ``ValueError`` when the object is empty (:issue:`48459`)
- Bug in :meth:`Series.groupby` raises ``ValueError`` when an entry of the index is equal to the name of the index (:issue:`48567`)
-

Reshaping
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ def is_in_obj(gpr) -> bool:
exclusions.add(gpr.name)

elif is_in_axis(gpr): # df.groupby('name')
if gpr in obj:
if obj.ndim != 1 and gpr in obj:
if validate:
obj._check_label_or_level_ambiguity(gpr, axis=axis)
in_axis, name, gpr = True, gpr, obj[gpr]
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2907,3 +2907,34 @@ def test_groupby_cumsum_mask(any_numeric_ea_dtype, skipna, val):
dtype=any_numeric_ea_dtype,
)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"val_in, index, val_out",
[
(
[1.0, 2.0, 3.0, 4.0, 5.0],
["foo", "foo", "bar", "baz", "blah"],
[3.0, 4.0, 5.0, 3.0],
),
(
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
["foo", "foo", "bar", "baz", "blah", "blah"],
[3.0, 4.0, 11.0, 3.0],
),
],
)
def test_groupby_index_name_in_index_content(val_in, index, val_out):
# GH 48567
series = Series(data=val_in, name="values", index=Index(index, name="blah"))
result = series.groupby("blah").sum()
expected = Series(
data=val_out,
name="values",
index=Index(["bar", "baz", "blah", "foo"], name="blah"),
)
tm.assert_series_equal(result, expected)

result = series.to_frame().groupby("blah").sum()
expected = expected.to_frame()
tm.assert_frame_equal(result, expected)