Skip to content

Commit b2aed4a

Browse files
author
T. Koskamp
committed
BUG: Inconsistent behavior of Groupby with None values with filter (#62501)
1 parent e982ba2 commit b2aed4a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

doc/source/whatsnew/v2.3.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Bug fixes
1414
^^^^^^^^^
1515
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
1616
- Bug in :meth:`Series.str.replace` raising an error on valid group references (``\1``, ``\2``, etc.) on series converted to PyArrow backend dtype (:issue:`62653`)
17+
- Bug in :meth:`~DataFrame.groupby` with ``None`` values with filter (:issue:`62501`)
1718

1819
.. ---------------------------------------------------------------------------
1920
.. _whatsnew_234.contributors:

pandas/core/groupby/groupby.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ def get_converter(s):
650650
return lambda key: Timestamp(key)
651651
elif isinstance(s, np.datetime64):
652652
return lambda key: Timestamp(key).asm8
653+
elif isna(s):
654+
return lambda key: np.nan
653655
else:
654656
return lambda key: key
655657

@@ -684,11 +686,17 @@ def get_converter(s):
684686
for name in names
685687
)
686688

689+
elif any(isna(k) for k in self.indices.keys()):
690+
converters = [get_converter(name) for name in names]
691+
names = (converter(name) for converter, name in zip(converters, names))
692+
687693
else:
688694
converter = get_converter(index_sample)
689695
names = (converter(name) for name in names)
690696

691-
return [self.indices.get(name, []) for name in names]
697+
indices = {np.nan if isna(k) else k: v for k, v in self.indices.items()}
698+
699+
return [indices.get(name, []) for name in names]
692700

693701
@final
694702
def _get_index(self, name):

0 commit comments

Comments
 (0)