Skip to content

Commit 83d6547

Browse files
authored
Merge pull request #267 from v-shaal/fix/bdeu-get-group-pandas2
Fix local_score_BDeu KeyError on pandas >= 2.0
2 parents 8f479e3 + d539221 commit 83d6547

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

causallearn/score/LocalScoreFunction.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ def local_score_BDeu(Data: ndarray, i: int, PAi: List[int], parameters=None) ->
196196
# calculate N_{ijk}: for each parent config, count occurrences of each X_i value
197197
Nijk_map = {}
198198
for ij in Nij_map_keys_list:
199-
group = Data_pd_group_Nij.get_group((ij,) if not isinstance(ij, tuple) else ij)
199+
# For a single grouper, pandas >= 2.0 expects the scalar key while
200+
# older pandas accepted a 1-tuple; try the scalar first and fall
201+
# back so BDeu works across pandas versions.
202+
try:
203+
group = Data_pd_group_Nij.get_group(ij)
204+
except (KeyError, ValueError):
205+
group = Data_pd_group_Nij.get_group((ij,) if not isinstance(ij, tuple) else ij)
200206
counts = group[xi_col].value_counts().reset_index()
201207
counts.columns = [xi_col, "times"]
202208
Nijk_map[ij] = counts

0 commit comments

Comments
 (0)