From 187246ea5978d437caa60a83407dcb80cb76b6e0 Mon Sep 17 00:00:00 2001 From: Jennifer A Clark Date: Thu, 19 Sep 2024 13:05:34 -0400 Subject: [PATCH] avoid pandas futurewarning for grouping with get_group() (#407) * fix #407 * Update pandas syntax to avoid FutureWarnings: FutureWarning: When grouping with a length-1 list-like, you will need to pass a length-1 tuple to get_group in a future version of pandas. Pass `(name,)` instead of `name` to silence this warning. * Update CHANGES --- CHANGES | 6 ++++-- src/alchemlyb/estimators/bar_.py | 19 ++++++++++++++++--- src/alchemlyb/estimators/mbar_.py | 6 +++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 1ada98db..11fe15a4 100644 --- a/CHANGES +++ b/CHANGES @@ -12,13 +12,14 @@ The rules for this file: * accompany each entry with github issue/PR number (Issue #xyz) * release numbers follow "Semantic Versioning" https://semver.org -09/19/2024 orbeckst +09/19/2024 orbeckst, jaclark5 * 2.4.1 Fixes - [doc] tutorial: use alchemlyb.concat (PR #399) - + - Resolve pandas FutureWarnings in bar_.py and mbar_.py (issue #408 PR #407) + 09/17/2024 jaclark5, orbeckst @@ -32,6 +33,7 @@ Enhancements Changes - modernize build system: replaced setup.py,cfg with pyproject.toml (#385) + 08/24/2024 xiki-tempula * 2.3.2 diff --git a/src/alchemlyb/estimators/bar_.py b/src/alchemlyb/estimators/bar_.py index 22bf958f..e0ce2647 100644 --- a/src/alchemlyb/estimators/bar_.py +++ b/src/alchemlyb/estimators/bar_.py @@ -97,7 +97,11 @@ def fit(self, u_nk): # group u_nk by lambda states groups = u_nk.groupby(level=u_nk.index.names[1:]) N_k = [ - (len(groups.get_group(i)) if i in groups.groups else 0) + ( + len(groups.get_group(i if isinstance(i, tuple) else (i,))) + if i in groups.groups + else 0 + ) for i in u_nk.columns ] @@ -119,12 +123,21 @@ def fit(self, u_nk): continue # get us from lambda step k - uk = groups.get_group(self._states_[k]) + uk = groups.get_group( + self._states_[k] + if isinstance(self._states_[k], tuple) + else (self._states_[k],) + ) # get w_F w_f = uk.iloc[:, k + 1] - uk.iloc[:, k] # get us from lambda step k+1 - uk1 = groups.get_group(self._states_[k + 1]) + uk1 = groups.get_group( + self._states_[k + 1] + if isinstance(self._states_[k + 1], tuple) + else (self._states_[k + 1],) + ) + # get w_R w_r = uk1.iloc[:, k] - uk1.iloc[:, k + 1] diff --git a/src/alchemlyb/estimators/mbar_.py b/src/alchemlyb/estimators/mbar_.py index ab35df14..45a648f6 100644 --- a/src/alchemlyb/estimators/mbar_.py +++ b/src/alchemlyb/estimators/mbar_.py @@ -127,7 +127,11 @@ def fit(self, u_nk): groups = u_nk.groupby(level=u_nk.index.names[1:]) N_k = [ - (len(groups.get_group(i)) if i in groups.groups else 0) + ( + len(groups.get_group(i if isinstance(i, tuple) else (i,))) + if i in groups.groups + else 0 + ) for i in u_nk.columns ] self._states_ = u_nk.columns.values.tolist()