From 337b41b0b326391515d9a411065e3a34a36f860e Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 5 Aug 2020 15:38:31 +0100 Subject: [PATCH] Ensure _group_selection_context is always reset Context managers will resume with an exception if the with block calling them fails. This happening is not an excuse to not clean up. --- pandas/core/groupby/groupby.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index ac45222625569..6c8a780859939 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -463,8 +463,10 @@ def _group_selection_context(groupby): Set / reset the _group_selection_context. """ groupby._set_group_selection() - yield groupby - groupby._reset_group_selection() + try: + yield groupby + finally: + groupby._reset_group_selection() _KeysArgType = Union[