Skip to content

BUG: Index.get_indexer casts values is given as list #55834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
Tracked by #54792
phofl opened this issue Nov 5, 2023 · 3 comments · Fixed by #56013
Closed
3 tasks done
Tracked by #54792

BUG: Index.get_indexer casts values is given as list #55834

phofl opened this issue Nov 5, 2023 · 3 comments · Fixed by #56013
Labels
API Design Arrow pyarrow functionality Strings String extension data type and string data
Milestone

Comments

@phofl
Copy link
Member

phofl commented Nov 5, 2023

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

pd.options.future.infer_string = True

idx = pd.Index(["a", "b", None], dtype="object")
idx.get_indexer([None, "x"])


[-1 -1]

Issue Description

We cast the iterable to an Index, which will cast None to NaN and thus the fixtures won't match anymore. I think it makes sense here to keep object if idx is also object dtype?

cc @jbrockmendel @jorisvandenbossche

Expected Behavior

See above

Installed Versions

Replace this line with the output of pd.show_versions()

@phofl phofl added API Design Strings String extension data type and string data Arrow pyarrow functionality labels Nov 5, 2023
@jorisvandenbossche
Copy link
Member

I would expect we coerce the list to an Index with the dtype of the calling index, but so that is not the case?

At least for object dtype we should do that, I think. Also without the string inference, you can already run into "issues" with this in case of mixed objects:

In [34]: idx = pd.Index(["a", 0.2, None], dtype="object")

In [35]: idx.get_indexer([0.2, None])
Out[35]: array([ 1, -1])

@phofl
Copy link
Member Author

phofl commented Nov 6, 2023

No we do the inference without taking the dtype of self into account

@jbrockmendel
Copy link
Member

I think it makes sense here to keep object if idx is also object dtype?

In most cases in get_indexer we end up casting self/target to a common dtype, which would be object in this case anyway. So avoiding the inference step wouldn't be so bad. The caveats that come to mind are 1) _maybe_downcast_for_indexing can downcast in some cases, 2) i think Categorical has some special handling, 3) in a perfect world we'd avoid that upcasting altogether

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Arrow pyarrow functionality Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants