8000 Groupby agg works with pd.Series.nunique, but groupby nunique fails with axis=1 by Ynob2000 · Pull Request #30311 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Groupby agg works with pd.Series.nunique, but groupby nunique fails with axis=1 #30311

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 11 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Suggested changes by jreback
  • Loading branch information
Nicolas Lepleux committed Dec 12, 2019
commit 0135842c53320f8a08a350723b2d0fb50fbd860e
5 changes: 1 addition & 4 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,7 @@ def get_grouper(
raise ValueError("multiple levels only valid with MultiIndex")

if isinstance(level, str):
if axis == 0:
axis_name = "index"
else:
axis_name = "columns"
axis_name = obj._get_axis_name(axis)
if getattr(obj, axis_name).name != level:
raise ValueError(
f&q 8000 uot;level name {level} is not the name of the {axis_name}"
Expand Down
23 changes: 7 additions & 16 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,27 +501,18 @@ def test_groupby_level(self, sort, mframe, df):
with pytest.raises(ValueError, match=msg):
df.groupby(level=1)

def test_groupby_level_index_names(self):
@pytest.mark.parametrize("axis", [0, 1])
def test_groupby_level_index_names(self, axis):
# GH4014 this used to raise ValueError since 'exp'>1 (in py2)
df = DataFrame({"exp": ["A"] * 3 + ["B"] * 3, "var1": range(6)}).set_index(
"exp"
)
df.groupby(level="exp")
msg = "level name foo is not the name of the index"
if axis:
df = df.T
df.groupby(level="exp", axis=axis)
msg = f"level name foo is not the name of the {df._get_axis_name(axis)}"
with pytest.raises(ValueError, match=msg):
df.groupby(level="foo")

def test_groupby_level_columns_names(self):
# This used to raise with 'level name exp is not the name of the index'
df = (
DataFrame({"exp": ["A"] * 3 + ["B"] * 3, "var1": range(6)})
.set_index("exp")
.T
)
df.groupby(level="exp", axis=1)
msg = "level name foo is not the name of the columns"
with pytest.raises(ValueError, match=msg):
df.groupby(level="foo", axis=1)
df.groupby(level="foo", axis=axis)

@pytest.mark.parametrize("sort", [True, False])
def test_groupby_level_with_nas(self, sort):
Expand Down
0