Repository navigation
Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
import pint_pandas
df = pd.DataFrame({"A": [0, 1]}, index=pd.Index(pd.array([1.0, 2.0], dtype="pint[m]")))
key = df.index[0] * 3 # <Quantity(3.0, 'meter')>, not in the index
df.loc[key] # TypeError: 'numpy.float64' object is not iterable (expected KeyError)
df.loc[key] = 2 # TypeError: 'numpy.float64' object is not iterable (expected row to be appended)Issue Description
Found this when unskipping some of pint-pandas' tests. In test_loc_setitem_with_expansion_preserves_ea_index_dtype, pandas tries to iterate over the Quantity as if it were a list
pandas/pandas/core/indexes/base.py
Lines 3844 to 3850 in 11f0a53
| except KeyError as err: | |
| if isinstance(casted_key, slice) or ( | |
| isinstance(casted_key, abc.Iterable) | |
| and any(isinstance(x, slice) for x in casted_key) | |
| ): | |
| raise InvalidIndexError(key) from err | |
| raise KeyError(key) from err |
change isinstance(casted_key, abc.Iterable) to is_list_like(casted_key)
Expected Behavior
see example above
Installed Versions
Details
``` INSTALLED VERSIONS ------------------ commit : e04b26f python : 3.13.5 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 151 Stepping 2, GenuineIntel byteorder : little LOCALE : English_United Kingdom.1252pandas : 3.0.1
numpy : 2.4.2
dateutil : 2.9.0.post0
IPython : 8.12.0
jinja2 : 3.1.6
matplotlib : 3.10.8
numexpr : 2.14.2
pyarrow : 23.0.1
pytest : 9.0.2
</details>
Activity
shahwazreza commentedon Nov 1, 2026
I can reproduce this on main.
is_list_likereturns False for the Quantity here since it treatsndim == 0objects as scalars, so swapping it in for theabc.Iterablecheck fixes both cases:df.loc[key]raisesKeyErroranddf.loc[key] = 2appends the row.I'll open a PR with that change and a regression test that uses a small iterable scalar instead of pint.
10k11 commentedon Nov 6, 2026
/take
github-actions commentedon Nov 6, 2026
Thanks for your interest, @10k11! This issue is still labeled
Needs Triage, which means it needs another look before work starts — so it isn't open to claim just yet. Please hold off on a PR until that label is removed. See the contributing guide for more.