Skip to content

Commit ec925d1

Browse files
committed
Fix pandas 1.5.3 compatibility issues
- Remove include_groups parameter from all groupby().apply() calls - Add explicit column selection to prevent deprecation warnings - Update AUC function to work with pandas 1.5.3 - Update GRADE function to work with pandas 1.5.3 - Update episode_calculation function to work with pandas 1.5.3 - Bump version to 0.3.1 The include_groups parameter was introduced in pandas 2.0.0, causing 'unexpected keyword argument' errors in pandas 1.5.3. This fix: 1. Removes all include_groups=False parameters 2. Adds explicit column selection [['gl', 'gl_next']] to groupby operations 3. Maintains same functionality while ensuring compatibility All functions now work correctly with pandas 1.5.3: - iglu.auc() - AUC calculation - iglu.grade() - GRADE score calculation - iglu.episode_calculation() - Episode analysis Resolves compatibility issues for users with older pandas versions.
1 parent 8932308 commit ec925d1

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

iglu_python/auc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def auc_single(subject_data: pd.DataFrame | pd.Series, tz: str = "") -> float:
8686
if is_iglu_r_compatible():
8787
input_data["day"] = input_data["time"].dt.floor("d")
8888
input_data["gl_next"] = input_data["gl"].shift(-1)
89-
each_day_area = input_data.groupby("day").apply(
90-
lambda x: np.nansum((dt0 / 60) * (x["gl"].values + x["gl_next"].values) / 2), include_groups=False
89+
each_day_area = input_data.groupby("day")[["gl", "gl_next"]].apply(
90+
lambda x: np.nansum((dt0 / 60) * (x["gl"].values + x["gl_next"].values) / 2)
9191
)
9292
# calculate number of not nan trapezoids in total (number of not nan gl and gl_next)
9393
n_trapezoids = (~np.isnan(input_data["gl"]) & ~np.isnan(input_data["gl_next"])).sum()
@@ -102,8 +102,8 @@ def auc_single(subject_data: pd.DataFrame | pd.Series, tz: str = "") -> float:
102102
input_data["gl_next"] = input_data["gl"].shift(-1)
103103

104104
# Calculate AUC for each hour using trapezoidal rule (mg*min/dL)
105-
hourly_auc = input_data.groupby("hour").apply(
106-
lambda x: np.nansum((dt0 / 60) * (x["gl"].values + x["gl_next"].values) / 2), include_groups=False
105+
hourly_auc = input_data.groupby("hour")[["gl", "gl_next"]].apply(
106+
lambda x: np.nansum((dt0 / 60) * (x["gl"].values + x["gl_next"].values) / 2)
107107
)
108108
# 0 mean no data in this hour, replace with nan
109109
hourly_auc = hourly_auc.replace(0, np.nan)

iglu_python/episode_calculation.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def episode_single(
292292

293293
# Classify events for each segment
294294
ep_per_seg = (
295-
segment_data.groupby("segment")
295+
segment_data.groupby("segment")[["gl"]]
296296
.apply(
297297
lambda x: pd.DataFrame(
298298
{
@@ -302,8 +302,7 @@ def episode_single(
302302
"lv2_hyper": event_class(x, "hyper", lv2_hyper, dur_idx, end_idx),
303303
"ext_hypo": event_class(x, "hypo", lv1_hypo, int(120 / dt0) + 1, end_idx),
304304
}
305-
),
306-
include_groups=False,
305+
)
307306
)
308307
.reset_index()
309308
.drop(columns=["level_1"])
@@ -383,7 +382,7 @@ def event_class(
383382

384383
# Group by event and calculate start/end positions
385384
annotated_grouped = (
386-
annotated.groupby("event")
385+
annotated.groupby("event")[["level"]]
387386
.apply(
388387
lambda x: pd.DataFrame(
389388
{
@@ -399,8 +398,7 @@ def event_class(
399398
["end" if (not x["level"].iloc[0] and len(x) >= end_duration) else None] + [None] * (len(x) - 1)
400399
),
401400
}
402-
),
403-
include_groups=False,
401+
)
404402
)
405403
.reset_index()
406404
.drop(columns=["level_1"])
@@ -462,12 +460,16 @@ def lv1_excl(data: pd.DataFrame) -> np.ndarray:
462460
lv2 = [column for column in data.columns if column.startswith("lv2")]
463461
lv2_first = lv2[0]
464462
# Group by segment and lv1
465-
grouped = data.groupby(["segment", lv1_first])
463+
grouped = data.groupby(["segment", lv1_first])[
464+
[
465+
lv1_first,
466+
lv2_first,
467+
]
468+
]
466469

467470
# Calculate exclusive labels
468471
excl = grouped.apply(
469-
lambda x: pd.DataFrame({"excl": [0 if (x[lv2_first].values > 0).any() else x[lv1_first].iloc[0]] * len(x)}),
470-
include_groups=False,
472+
lambda x: pd.DataFrame({"excl": [0 if (x[lv2_first].values > 0).any() else x[lv1_first].iloc[0]] * len(x)})
471473
)
472474

473475
excl = excl.reset_index()

iglu_python/grade.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ def grade(data: Union[pd.DataFrame, pd.Series]) -> pd.DataFrame:
6161
data = check_data_columns(data)
6262

6363
# Calculate GRADE score for each subject
64-
result = (
65-
data.groupby("id")
66-
.apply(lambda x: np.mean(_grade_formula(x["gl"].dropna())), include_groups=False)
67-
.reset_index()
68-
)
64+
result = data.groupby("id")[["gl"]].apply(lambda x: np.mean(_grade_formula(x["gl"].dropna()))).reset_index()
6965
result.columns = ["id", "GRADE"]
7066

7167
return result

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "iglu_python"
7-
version = "0.3.0"
7+
version = "0.3.1"
88
description = "Python implementation of the iglu package for continuous glucose monitoring data analysis"
99
readme = "README.md"
1010
requires-python = ">=3.11"

0 commit comments

Comments
 (0)