Skip to content

Fix multiindex loc nan #43943

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
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
549386c
fix problem without adding tests
CloseChoice Oct 9, 2021
81970c7
add test, still not all tests running
CloseChoice Oct 9, 2021
8443c62
remove print and debug statements
CloseChoice Oct 10, 2021
9eae773
Fixes from pre-commit [automated commit]
CloseChoice Oct 10, 2021
c497e34
add new test
CloseChoice Oct 10, 2021
fde027b
update test as desired in PR comments
CloseChoice Oct 10, 2021
0ef5d04
Merge branch 'FIX-multiindex-loc-nan' of github.com:CloseChoice/panda…
CloseChoice Oct 10, 2021
246a421
add whatsnew entry
CloseChoice Oct 10, 2021
bbeeb1b
fix wrong issue number
CloseChoice Oct 10, 2021
6b38ba4
save nan encoding in new state variable and replace it before returni…
CloseChoice Oct 12, 2021
d04b7b8
remove wrong whatsnew entry
CloseChoice Oct 12, 2021
cc75b44
remove debug statement
CloseChoice Oct 12, 2021
c88b2d8
add whatsnew entry
CloseChoice Oct 13, 2021
0a3ce38
Merge branch 'FIX-multiindex-loc-nan' of github.com:CloseChoice/panda…
CloseChoice Oct 13, 2021
ed537cc
Merge branch 'master' into FIX-multiindex-loc-nan
CloseChoice Oct 17, 2021
77e7283
fix wrong code quotes in whatsnew
CloseChoice Oct 17, 2021
72fb026
WIP: add support for (pd.NA, pd.NaT, np.datetime64("NaT", "ns"), np.t…
CloseChoice Oct 22, 2021
cf0355d
WIP: add support for (pd.NA, pd.NaT, np.datetime64("NaT", "ns"), np.t…
CloseChoice Oct 22, 2021
427a515
Fixes from pre-commit [automated commit]
CloseChoice Oct 22, 2021
29c37b6
fix problem without adding tests
CloseChoice Oct 9, 2021
d274bca
add test, still not all tests running
CloseChoice Oct 9, 2021
f72cbf7
remove print and debug statements
CloseChoice Oct 10, 2021
7ab0c9c
Fixes from pre-commit [automated commit]
CloseChoice Oct 10, 2021
a758d44
add new test
CloseChoice Oct 10, 2021
c3ec7f0
update test as desired in PR comments
CloseChoice Oct 10, 2021
245b141
add whatsnew entry
CloseChoice Oct 10, 2021
ae706c8
fix wrong issue number
CloseChoice Oct 10, 2021
30097ca
save nan encoding in new state variable and replace it before returni…
CloseChoice Oct 12, 2021
ef951a6
remove wrong whatsnew entry
CloseChoice Oct 12, 2021
0fa1784
remove debug statement
CloseChoice Oct 12, 2021
1f78023
add whatsnew entry
CloseChoice Oct 13, 2021
5b3c61d
fix wrong code quotes in whatsnew
CloseChoice Oct 17, 2021
98c8f13
WIP: add support for (pd.NA, pd.NaT, np.datetime64("NaT", "ns"), np.t…
CloseChoice Oct 22, 2021
ec218d5
WIP: add support for (pd.NA, pd.NaT, np.datetime64("NaT", "ns"), np.t…
CloseChoice Oct 22, 2021
d92cc60
Merge branch 'master' into FIX-multiindex-loc-nan
CloseChoice Nov 6, 2021
4dda020
Merge branch 'FIX-multiindex-loc-nan' of github.com:CloseChoice/panda…
CloseChoice Nov 6, 2021
6850b73
add tests for issue 36060
CloseChoice Nov 6, 2021
e7a52e3
Fixes from pre-commit [automated commit]
CloseChoice Nov 6, 2021
9682350
changes according to PR discussions
CloseChoice Nov 7, 2021
0ad2583
Merge branch 'FIX-multiindex-loc-nan' of github.com:CloseChoice/panda…
CloseChoice Nov 7, 2021
cb92f95
use assert_series_equal in test_groupby_dropna
CloseChoice Nov 7, 2021
e3c734d
make _has_na_placeholder cache_readonly
CloseChoice Nov 10, 2021
cf48e70
make _has_na_placeholder cache_readonly
CloseChoice Nov 10, 2021
49b1d65
Fixes from pre-commit [automated commit]
CloseChoice Nov 10, 2021
d00b39f
remove commented out stuff
CloseChoice Nov 10, 2021
a97ef1a
Merge branch 'FIX-multiindex-loc-nan' of github.com:CloseChoice/panda…
CloseChoice Nov 10, 2021
d180901
remove unnecessary import
CloseChoice Nov 10, 2021
cc6af9d
WIP: intermediate commit for loop solution
CloseChoice Nov 16, 2021
02bf699
changes for static analysis checks
CloseChoice Nov 16, 2021
7211984
Merge branch 'master' of github.com:pandas-dev/pandas into FIX-multii…
CloseChoice Dec 8, 2021
4520444
fix imports
CloseChoice Dec 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -735,11 +735,15 @@ cdef class BaseMultiIndexCodesEngine:
raise TypeError(f"'{key}' is an invalid key")
if not isinstance(key, tuple):
raise KeyError(key)
try:
indices = [0 if checknull(v) else lev.get_loc(v) + 1
for lev, v in zip(self.levels, key)]
except KeyError:
raise KeyError(key)
indices = []
for k, lev in zip(key, self.levels):
try:
indices.append(lev.get_loc(k) + 1)
except KeyError as err:
if checknull(k):
indices.append(0)
else:
raise KeyError(key) from err

# Transform indices into single integer:
lab_int = self._codes_to_ints(np.array(indices, dtype='uint64'))
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexing/multiindex/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,18 @@ def test_loc_empty_multiindex():
result = df
expected = DataFrame([1, 2, 3, 4], index=index, columns=["value"])
tm.assert_frame_equal(result, expected)


def test_loc_nan_multiindex():
df = DataFrame(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number as a comment here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are declaring this a regression, we should maybe have a test that previously passed. In what previous version of pandas did this test pass?

{
"temp_playlist": [0, 0, 0, 0],
"objId": ["o1", np.nan, "o1", np.nan],
"x": [1, 2, 3, 4],
}
)

agg_df = df.groupby(by=["temp_playlist", "objId"], dropna=False)["x"].agg(list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize on dropna=True & False

result = agg_df.loc[agg_df.index[-1]]
expected = [2, 4]
assert result == expected