Skip to content

Fix kwarg_help() crash on current pandas (trailing-comma .loc indexing) - #703

Open
heykav wants to merge 1 commit into
matplotlib:masterfrom
heykav:fix/kwarg-help-loc-trailing-comma
Open

heykav wants to merge 1 commit into
matplotlib:masterfrom
heykav:fix/kwarg-help-loc-trailing-comma

Conversation

@heykav

@heykav heykav commented Sep 24, 2026

Copy link
Copy Markdown

Bug

mpf.kwarg_help() raises ValueError: zip() argument 2 is longer than argument 1 on current pandas (tested with pandas 3.0.6, but the same failure occurs on any pandas 2.x that enforces strict=True zip in _LocIndexer._multi_take). This is a 100% reproduction on the existing tests/test_kwarg_help.py::test_kwarg_help test.

Root cause

df_wrapcols() in src/mplfinance/_kwarg_help.py selects each row with:

row = df.loc[ix,]

The trailing comma turns ix, into the 1-tuple (ix,). Older pandas treated a 1-tuple key to .loc the same as the bare scalar. Current pandas routes any tuple key through _getitem_tuple -> _multi_take, which does:

d = {axis: self._get_listlike_indexer(key, axis)
     for (key, axis) in zip(tup, self.obj._AXIS_ORDERS, strict=True)}

self.obj._AXIS_ORDERS for a DataFrame has length 2 (index, columns), but tup here has length 1, so zip(..., strict=True) raises ValueError. Minimal repro outside mplfinance:

import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df.loc[0,]   # ValueError: zip() argument 2 is longer than argument 1

Fix

Drop the stray trailing comma so the key is a plain scalar, which is what was actually intended (df.loc[ix]), and is a strict subset of the existing behavior — no other code path is touched.

Verification

  • tests/test_kwarg_help.py::test_kwarg_help: fails on master (ValueError: zip() argument 2 is longer than argument 1), passes after this change.
  • Red/green confirmed by reverting the one-line change and re-running: fails without the fix, passes with it.
  • Full tests/ suite: 43 failed / 8 passed before -> 42 failed / 9 passed after, with no new failures introduced. The remaining failures are pre-existing and unrelated (mostly image-comparison baseline drift from newer matplotlib rendering, and an ATR/.loc indexing bug in the P&F/renko code path that is already tracked in open PR Add traditional box scaling for P&F charts and fix ATR indexing bug #699).

Environment used for verification: fresh clone, Python 3.12 venv, pip install -e ., pandas 3.0.6 / numpy 2.5.3 / matplotlib 3.11.2 (current latest as of testing).

🤖 Generated with Claude Code

… indexing

df_wrapcols() in _kwarg_help.py indexed rows with `df.loc[ix,]`. The
trailing comma turns the key into a 1-tuple, which older pandas
silently treated the same as `df.loc[ix]`, but current pandas treats
any tuple key to .loc as multi-axis indexing and zips it against
DataFrame._AXIS_ORDERS (length 2) with strict=True, raising
`ValueError: zip() argument 2 is longer than argument 1` for a
1-tuple. This broke every call to mpf.kwarg_help(), including the one
covered by tests/test_kwarg_help.py.

Fix: drop the stray trailing comma so the key is a plain scalar
(`df.loc[ix]`), which is what was actually intended and what pandas'
row-selection API expects.

Verified: tests/test_kwarg_help.py::test_kwarg_help now passes (was
failing before the fix); confirmed red before / green after by
reverting and reapplying the change. Full test suite: 43 failed / 8
passed before -> 42 failed / 9 passed after, with no new failures
introduced (remaining failures are pre-existing, unrelated pandas/
matplotlib compatibility issues, including one already tracked in
open PR matplotlib#699).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant