mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-27 23:47:46 +00:00
fix(auto-fixer): strip spurious .reset_index() after .transform() calls
LLM sometimes copies the .reset_index(level=N, drop=True) suffix from groupby().rolling().method() patterns and adds it after .transform(), but transform() already preserves the original index. The extra reset_index() drops an index level and causes ValueError: 'cannot reindex on an axis with duplicate labels' or shape mismatch on assignment. Detect: any line containing both .transform( and .reset_index(level=..., drop=True) Fix: strip the .reset_index() suffix from those lines. Adds 1 new test (test_transform_reset_index_stripped) — total 30 tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -654,6 +654,25 @@ class FactorAutoFixer:
|
||||
f"groupby: {df_var}.groupby(level={level})['{col}'].apply() → transform()"
|
||||
)
|
||||
|
||||
# === FIX: .transform(...).reset_index(level=N, drop=True) ===
|
||||
# transform() already returns the same index as the input — adding reset_index()
|
||||
# after it drops an index level and causes ValueError on assignment back to df['col'].
|
||||
# Detected line-by-line: if a line contains both .transform( and .reset_index(level=
|
||||
reset_suffix = re.compile(r'\s*\.reset_index\s*\(\s*level\s*=[^,)]+,\s*drop\s*=\s*True\s*\)\s*$')
|
||||
new_lines = []
|
||||
changed = False
|
||||
for line in fixed_code.splitlines():
|
||||
if '.transform(' in line and '.reset_index(' in line:
|
||||
cleaned = reset_suffix.sub('', line)
|
||||
if cleaned != line:
|
||||
new_lines.append(cleaned)
|
||||
changed = True
|
||||
continue
|
||||
new_lines.append(line)
|
||||
if changed:
|
||||
fixed_code = '\n'.join(new_lines)
|
||||
self.fixes_applied.append("groupby: removed spurious .reset_index() after .transform()")
|
||||
|
||||
# Pattern: Simple groupby().apply() with rolling().method()
|
||||
# df.groupby(level=N).apply(lambda x: x['col'].rolling(...).method())
|
||||
apply_pattern = r"df\.groupby\(level=(\d+)\)\.apply\(\s*lambda\s+x:\s+x\['([^']+)'\]\.rolling\([^)]+\)\.(\w+)\([^)]*\)\s*\)"
|
||||
|
||||
Reference in New Issue
Block a user