Skip to content

API: Series[index_with_no_matches] vs Series[list_with_no_matches] #33642

Closed
@jbrockmendel

Description

@jbrockmendel

We treat list indexers differently from array-like indexers:

ser = pd.Series(["A", "B"])
key = pd.Series(["C"])

>>> ser[key]
C    NaN
dtype: object

>>> ser[pd.Index(key)]
C    NaN
dtype: object

>>> ser[np.array(key)]
C    NaN
dtype: object

>>> ser[list(key)]
Traceback (most recent call last):
[...]
  File "/Users/bmendel/Desktop/pd/pandas/pandas/core/indexing.py", line 1312, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['C'], dtype='object')] are in the [index]"

Also inconsistent because ser.loc[key] raises for all 4 cases.

Is there a compelling reason for this? I tried making all of these behave like the list case and only one test broke (that test being the example above). The test was added in #5880.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions