Skip to content
Open
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.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ including other versions of pandas.
Bug fixes
^^^^^^^^^
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Fix bug in :meth:`~DataFrame.groupby` with ``None`` values with filter (:issue:`62501`)

.. ---------------------------------------------------------------------------
.. _whatsnew_234.contributors:
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ def get_converter(s):
return lambda key: Timestamp(key)
elif isinstance(s, np.datetime64):
return lambda key: Timestamp(key).asm8
elif isna(s):
return lambda key: np.nan
else:
return lambda key: key

Expand Down Expand Up @@ -684,11 +686,17 @@ def get_converter(s):
for name in names
)

elif any(isna(k) for k in self.indices.keys()):
Copy link
Member

Choose a reason for hiding this comment

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

This check is expensive - this function is only ever called currently with names a list of length 1, and the rest of the method is O(1) in terms of self.indices. It's called from the inner loop of DataFramGroupBy.fitler as we're iterating over each group. This seems avoidable.

I believe we could change this function to just accept a single name (rather than a list) and then have a special case:

if isna(name):
    return self.indices.get(np.nan, [])

converters = [get_converter(name) for name in names]
names = (converter(name) for converter, name in zip(converters, names))

else:
converter = get_converter(index_sample)
names = (converter(name) for name in names)

return [self.indices.get(name, []) for name in names]
indices = {np.nan if isna(k) else k: v for k, v in self.indices.items()}
Copy link
Member

Choose a reason for hiding this comment

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

It seems better to do this on indices cached property directly, and only in the case where there is a NaN value with if not self.dropna and self.result_index.hasnans.


return [indices.get(name, []) for name in names]

@final
def _get_index(self, name):
Expand Down
Loading