Skip to content

BUG: Can't set a pint quantity scalar as index #71835

Open

Description

@andrewgsavage

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

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.1252

pandas : 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

  1. shahwazreza commented on Nov 1, 2026

    @shahwazreza

    I can reproduce this on main. is_list_like returns False for the Quantity here since it treats ndim == 0 objects as scalars, so swapping it in for the abc.Iterable check fixes both cases: df.loc[key] raises KeyError and df.loc[key] = 2 appends the row.

    I'll open a PR with that change and a regression test that uses a small iterable scalar instead of pint.

  2. 10k11 commented on Nov 6, 2026

    @10k11

    /take

  3. github-actions commented on Nov 6, 2026

    @github-actions
    Contributor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions