Skip to content

Commit e405863

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

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
@@ -649,6 +649,8 @@ def get_converter(s):
649649
return lambda key: Timestamp(key)
650650
elif isinstance(s, np.datetime64):
651651
return lambda key: Timestamp(key).asm8
652+
elif isna(s):
653+
return lambda key: np.nan
652654
else:
653655
return lambda key: key
654656

@@ -683,11 +685,17 @@ def get_converter(s):
683685
for name in names
684686
)
685687

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

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

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

0 commit comments

Comments
 (0)