Skip to content

fix: replace .max()[0] with .max().iloc[0] for pandas v2.x compatibility#7

Open
takuan517 wants to merge 2 commits into
In-Network-Machine-Learning:mainfrom
takuan517:fix/pandas-max-iloc
Open

fix: replace .max()[0] with .max().iloc[0] for pandas v2.x compatibility#7
takuan517 wants to merge 2 commits into
In-Network-Machine-Learning:mainfrom
takuan517:fix/pandas-max-iloc

Conversation

@takuan517

@takuan517 takuan517 commented Mar 10, 2026

Copy link
Copy Markdown

Summary

Fix a KeyError: 0 crash that occurs across all ML modules when running Planter with pandas >= 2.0.

Root Cause

packages.txt specifies pandas==1.1.3, where .max()[0] happens to work. However, pandas v2.0 removed support for positional integer indexing on Series, so the following pattern raises KeyError: 0 on modern environments (e.g., Ubuntu 24.04 with Python 3.12):

t_t = [test_X[[i]].max()[0], train_X[[i]].max()[0]]

.max() returns a Series indexed by column name, not by position.
Using [0] attempts to look up a column named 0, which does not exist.

Fix

Replace .max()[0] with .max().iloc[0], which uses explicit positional indexing and is compatible with both pandas v1. x and v2. x.

Affected Files

51 table_generator.py files across all ML modules: RF, DT, XGB, SVM, KNN, KM, NN, Bayes, IF, PCA, Autoencoder

Tested

  • Python 3.12, pandas 2.x, Ubuntu 24.04 ARM
  • RF/Type_EB on Iris dataset: accuracy 0.9556 ✓ (Matrix 1–3 all passing)

@aqn96

aqn96 commented Mar 16, 2026

Copy link
Copy Markdown

Can confirm this fix works. I independently hit the same KeyError: 0 on DT/Type_4 while running Planter on Ubuntu 24.04 ARM with Python 3.12 and pandas 3.0.1. .max().iloc[0] resolves the crash and results are correct.

Heads up — there are also 6 instances of .min()[0] that have the same breakage on pandas v2.x+:

grep -rn "\.min()\[0\]" src/ --include="*.py"
src/models/KNN/Type_EB/table_generator.py:209
src/models/KNN/Type_1/table_generator.py:208
src/models/KM/Type_LB/table_generator.py:63
src/models/KM/Type_EB/table_generator.py:213
src/models/KM/Type_clustreams/table_generator.py:213
src/models/KM/Type_1/table_generator.py:63

Might be worth including .min()[0].min().iloc[0] in this PR as well.

@takuan517

Copy link
Copy Markdown
Author

@aqn96
Thanks for confirming! Good catch on .min()[0] — I've updated the PR to include that fix across the 6 affected files as well.a6c6e34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants