Skip to content

Commit e44cc53

Browse files
jeremymanningclaude
andcommitted
Fix format2tidy KeyError by using dynamic column name
The pd.melt(df.T) call produces column names based on the DataFrame's MultiIndex level names, which may not always be 'Subject'. This fix dynamically retrieves the actual column name after melt instead of hardcoding it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 37d27d7 commit e44cc53

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

quail/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ def make_multi_index(listindex, sub_num):
5353

5454
def format2tidy(df, subjname, listname, subjgroup, analysis=None, position=0):
5555
melted_df = pd.melt(df.T)
56+
# Get the actual column name for subjects (first column after melt)
57+
# This may be 'Subject' or the actual index level name from the DataFrame
58+
subject_col = melted_df.columns[0]
5659
melted_df[subjname]=""
57-
for idx,sub in enumerate(melted_df['Subject'].unique()):
58-
melted_df.loc[melted_df['Subject']==sub,subjname]=subjgroup[idx]
60+
for idx,sub in enumerate(melted_df[subject_col].unique()):
61+
melted_df.loc[melted_df[subject_col]==sub,subjname]=subjgroup[idx]
5962
if analysis=='spc':
6063
base = list(df.columns)
6164
melted_df['Position'] = base * int(melted_df.shape[0] / len(base))

0 commit comments

Comments
 (0)