Skip to content
Closed
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
5 changes: 5 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ def _get_indices(self, names):
Safe get multiple indices, translate keys for
datelike to underlying repr.
"""
assert len(names) == 1
if isna(names[0]):
return [self.indices.get(np.nan, [])]
if isinstance(names[0], tuple):
names[0] = tuple(np.nan if isna(comp) else comp for comp in names[0])

def get_converter(s):
# possibly convert to the actual key types
Expand Down
20 changes: 17 additions & 3 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,23 @@ def indices(self) -> dict[Hashable, npt.NDArray[np.intp]]:
"""dict {group name -> group indices}"""
if len(self.groupings) == 1 and isinstance(self.result_index, CategoricalIndex):
# This shows unused categories in indices GH#38642
return self.groupings[0].indices
codes_list = [ping.codes for ping in self.groupings]
return get_indexer_dict(codes_list, self.levels)
result = self.groupings[0].indices
else:
codes_list = [ping.codes for ping in self.groupings]
result = get_indexer_dict(codes_list, self.levels)
if not self.dropna:
has_mi = isinstance(self.result_index, MultiIndex)
if not has_mi and self.result_index.hasnans:
result = {
np.nan if isna(key) else key: value for key, value in result.items()
}
elif has_mi:
# MultiIndex has no efficient way to tell if there are NAs
result = {
tuple(np.nan if isna(comp) else comp for comp in key): value
for key, value in result.items()
}
return result

@final
@cache_readonly
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/groupby/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,33 @@ def test_filter_consistent_result_before_after_agg_func():
grouper.sum()
result = grouper.filter(lambda x: True)
tm.assert_frame_equal(result, expected)


def test_filter_with_non_values():
# GH 62501
df = DataFrame(
[
[1],
[None],
],
columns=["a"],
)

result = df.groupby("a", dropna=False).filter(lambda x: True)
tm.assert_frame_equal(result, df)


def test_filter_with_non_values_multi_index():
# GH 62501
df = DataFrame(
[
[1, 2],
[3, None],
[None, 4],
[None, None],
],
columns=["a", "b"],
)

result = df.groupby(["a", "b"], dropna=False).filter(lambda x: True)
tm.assert_frame_equal(result, df)
Loading