Skip to content

pd.MultiIndex.get_loc(np.nan) #19132

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
toobaz opened this issue Jan 8, 2018 · 2 comments · Fixed by #28919
Closed

pd.MultiIndex.get_loc(np.nan) #19132

toobaz opened this issue Jan 8, 2018 · 2 comments · Fixed by #28919
Labels
Index Related to the Index class or subclasses Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@toobaz
Copy link
Member

toobaz commented Jan 8, 2018

Code Sample, a copy-pastable example if possible

In [2]: mi = pd.MultiIndex.from_product([[np.nan, 1]] * 3)

In [3]: mi.get_loc(np.nan)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/pietro/nobackup/repo/pandas/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2513             try:
-> 2514                 return self._engine.get_loc(key)
   2515             except KeyError:

/home/pietro/nobackup/repo/pandas/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5834)()

/home/pietro/nobackup/repo/pandas/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5639)()

/home/pietro/nobackup/repo/pandas/pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type (pandas/_libs/index.c:19008)()

KeyError: nan

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-641c024055e9> in <module>()
----> 1 mi.get_loc(np.nan)

/home/pietro/nobackup/repo/pandas/pandas/core/indexes/multi.py in get_loc(self, key, method)
   2121 
   2122         if not isinstance(key, tuple):
-> 2123             loc = self._get_level_indexer(key, level=0)
   2124             return _maybe_to_slice(loc)
   2125 

/home/pietro/nobackup/repo/pandas/pandas/core/indexes/multi.py in _get_level_indexer(self, key, level, indexer)
   2411         else:
   2412 
-> 2413             loc = level_index.get_loc(key)
   2414             if isinstance(loc, slice):
   2415                 return loc

/home/pietro/nobackup/repo/pandas/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2514                 return self._engine.get_loc(key)
   2515             except KeyError:
-> 2516                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2517 
   2518         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

/home/pietro/nobackup/repo/pandas/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5834)()

/home/pietro/nobackup/repo/pandas/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5639)()

/home/pietro/nobackup/repo/pandas/pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type (pandas/_libs/index.c:19008)()

KeyError: nan

Problem description

Opening a separate issue from #18485 (full label containing NaN) because, differently from that, this is not fixed by #19074.

xref #5286 because they are probably worth testing together.

Expected Output

slice(0, 4, None)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-5-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.22.0.dev0+448.gdbec3c92e
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.18.1
pyarrow: None
xarray: None
IPython: 5.2.2
sphinx: None
patsy: 0.4.1+dev
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: None
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Jan 9, 2018

yeah, nan handling is a bit of a problem. try this with multiple nan as well in say the 2nd level

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Difficulty Advanced Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Jan 9, 2018
@jreback jreback added this to the Next Major Release milestone Jan 9, 2018
@toobaz toobaz added Index Related to the Index class or subclasses and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 29, 2019
@proost
Copy link
Contributor

proost commented Sep 27, 2019

I would like to take this issue if no one handle this.

proost added a commit to proost/pandas that referenced this issue Oct 4, 2019
MultiIndex.get_loc can find nan when input is NA value(e.g. nan, None)
proost added a commit to proost/pandas that referenced this issue Oct 4, 2019
MultiIndex.get_loc can find nan when input is NA value(e.g. nan, None)
proost added a commit to proost/pandas that referenced this issue Oct 8, 2019
MultiIndex.get_loc can find missing value in MultiIndex with a null
value as input
proost added a commit to proost/pandas that referenced this issue Oct 9, 2019
MultiIndex.get_loc can find missing value in MultiIndex with a null
value as input
proost added a commit to proost/pandas that referenced this issue Oct 9, 2019
MultiIndex.get_loc can find missing value in MultiIndex with a null
value as input
proost added a commit to proost/pandas that referenced this issue Oct 10, 2019
MultiIndex.get_loc can't find nan with a null
value as input
proost added a commit to proost/pandas that referenced this issue Oct 11, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Oct 11, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Oct 11, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 1, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 5, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 21, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 21, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 21, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 21, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 22, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 22, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 22, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Nov 22, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
proost added a commit to proost/pandas that referenced this issue Dec 2, 2019
MultiIndex.get_loc could not find nan with values including missing
values as a input
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
4 participants