Skip to content

BUG: get_indexer_for() does not always return list of indeces #44084

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
johannes-mueller opened this issue Oct 18, 2021 · 1 comment · Fixed by #44404
Closed
3 tasks done

BUG: get_indexer_for() does not always return list of indeces #44084

johannes-mueller opened this issue Oct 18, 2021 · 1 comment · Fixed by #44404
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Interval Interval data type MultiIndex
Milestone

Comments

@johannes-mueller
Copy link
Contributor

johannes-mueller commented Oct 18, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

interval_index = pd.IntervalIndex.from_tuples([(1.0, np.inf), (-np.inf, 0.0), (0.0, 1.)], name='interval')
foo_index = pd.Index([1, 2, 3], name='foo')


multi_index = pd.MultiIndex.from_product([foo_index, interval_index])

print(multi_index.get_level_values('foo').get_indexer_for([2]))
print(multi_index.get_level_values('interval').get_indexer_for([pd.Interval(-np.inf, 0.0)]))


multi_index = pd.MultiIndex.from_product([interval_index, foo_index])

print(multi_index.get_level_values('foo').get_indexer_for([2]))
print(multi_index.get_level_values('interval').get_indexer_for([pd.Interval(-np.inf, 0.0)]))

Issue Description

get_indexer_for() does not return a list of indeces as described in the docs but a mask like list under some circumstances.

As far as I have investigated, the behavior occurs if all of the following conditions are met:

  • the index is a MultiIndex
  • the level in question is a IntervalIndex
  • the level in question is not the first level of the MultiIndex

The script above outputs

[3 4 5]
[0 1 0 0 1 0 0 1 0]
[1 4 7]
[3 4 5]

Expected Behavior

Expected output

[3 4 5]
[1 4 7]
[1 4 7]
[3 4 5]

Installed Versions

INSTALLED VERSIONS

commit : 3a6d4cd
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-88-lowlatency
Version : #99-Ubuntu SMP PREEMPT Thu Sep 23 18:30:52 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 1.4.0.dev0+933.g3a6d4cd01d
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : 6.2.5
hypothesis : 6.23.3
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@johannes-mueller johannes-mueller added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 18, 2021
@johannes-mueller johannes-mueller changed the title BUG: BUG: get_indexer_for() does not always return list of indeces Oct 18, 2021
@jbrockmendel
Copy link
Member

PR to straighten this out would be welcome

@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Oct 21, 2021
@mroeschke mroeschke added Interval Interval data type MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 30, 2021
johannes-mueller added a commit to boschresearch/pandas that referenced this issue Nov 12, 2021
…ev#44084)

GH#44084 boils down to the following.

According to the docs `.get_indexer_non_unique()` is supposed to return
"integers from 0 to n - 1 indicating that the index at these positions matches
the corresponding target values".  However, for an index that is non unique and
non monotonic it returns a boolean mask.  That is because it uses `.get_loc()`
which for non unique, non monotonic indexes returns a boolean mask.

This patch catches that case and converts the boolean mask from `.get_loc()`
into the corresponding array of integers if the index is not unique and not
monotonic.
johannes-mueller added a commit to boschresearch/pandas that referenced this issue Nov 12, 2021
@jreback jreback added this to the 1.4 milestone Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Interval Interval data type MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants