Skip to content

Commit

Permalink
avoid pandas futurewarning for grouping with get_group() (#407)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jaclark5 authored Sep 19, 2024
1 parent aba3671 commit 187246e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions src/alchemlyb/estimators/bar_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Expand All @@ -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]

Expand Down
6 changes: 5 additions & 1 deletion src/alchemlyb/estimators/mbar_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 187246e

Please sign in to comment.