Skip to content

BUG: Fix groupby nth with axis=1 #43926

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 16 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 16 additions & 7 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,18 +2545,25 @@ def nth(
# Drop NA values in grouping
mask = mask & (ids != -1)

out = self._selected_obj[mask]
if self.axis == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually let's make a method

self._selected_obj_for_axis(mask: np.ndarray[bool] | None)

as i think we use this in several places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually let's make a method

self._selected_obj_for_axis(mask: np.ndarray[bool] | None)

as i think we use this in several places.

@jreback, I had already factored these out in my other branch but I have done it here too, and made them both _mask_selected_obj(mask)

out = self._selected_obj[mask]
else:
out = self._selected_obj.iloc[:, mask]

if not self.as_index:
return out

result_index = self.grouper.result_index
out.index = result_index[ids[mask]]
if self.axis == 0:
out.index = result_index[ids[mask]]
if not self.observed and isinstance(result_index, CategoricalIndex):
out = out.reindex(result_index)

if not self.observed and isinstance(result_index, CategoricalIndex):
out = out.reindex(result_index)
out = self._reindex_output(out)
else:
out.columns = result_index[ids[mask]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we re-use any of the _wrap_foo_result methods?


out = self._reindex_output(out)
return out.sort_index() if self.sort else out
return out.sort_index(axis=self.axis) if self.sort else out

# dropna is truthy
if isinstance(n, valid_containers):
Expand Down Expand Up @@ -2599,7 +2606,9 @@ def nth(
mutated=self.mutated,
)

grb = dropped.groupby(grouper, as_index=self.as_index, sort=self.sort)
grb = dropped.groupby(
grouper, as_index=self.as_index, sort=self.sort, axis=self.axis
)
sizes, result = grb.size(), grb.nth(n)
mask = (sizes < max_len)._values

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/groupby/test_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,26 @@ def test_groupby_last_first_nth_with_none(method, nulls_fixture):
result = getattr(data, method)()

tm.assert_series_equal(result, expected)


def test_groupby_nth_with_column_axis():
# GH43926
df = DataFrame(
[
[4, 5, 6],
[8, 8, 7],
],
index=["z", "y"],
columns=["C", "B", "A"],
)
result = df.groupby(df.iloc[1], axis=1).nth(0)
expected = DataFrame(
[
[6, 4],
[7, 8],
],
index=["z", "y"],
columns=[7, 8],
)
expected.columns.name = "y"
tm.assert_frame_equal(result, expected)