From 197ac839b5f2e629d079295fb9bdd81ab7bccd43 Mon Sep 17 00:00:00 2001 From: "T. Koskamp" Date: Sun, 23 Nov 2025 12:52:15 +0100 Subject: [PATCH] BUG: Inconsistent behavior of Groupby with None values with filter (#62501) --- doc/source/whatsnew/v2.3.4.rst | 1 + pandas/core/groupby/groupby.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.3.4.rst b/doc/source/whatsnew/v2.3.4.rst index 836cea026d702..42c93a4a71f6a 100644 --- a/doc/source/whatsnew/v2.3.4.rst +++ b/doc/source/whatsnew/v2.3.4.rst @@ -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: diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index f7868b9e46c37..28654bee25e39 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -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 @@ -684,11 +686,17 @@ def get_converter(s): for name in names ) + elif any(isna(k) for k in self.indices.keys()): + 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()} + + return [indices.get(name, []) for name in names] @final def _get_index(self, name):