Skip to content

Bug in loc raised Error when non-integer slice was given for MultiIndex #37707

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

Merged
merged 31 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c8df1c8
Bug in loc raised Error when non integer interval was given for Multi…
phofl Nov 9, 2020
9e62c50
Adjust tests
phofl Nov 9, 2020
8377a79
Comment it back in
phofl Nov 9, 2020
e6acaac
Run black
phofl Nov 9, 2020
37fe677
Rename test
phofl Nov 9, 2020
550ac88
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 9, 2020
d38c9e7
Change new implementation
phofl Nov 9, 2020
72345a4
Do slicing outside
phofl Nov 9, 2020
04056ae
Add test
phofl Nov 9, 2020
41dbbcc
Change issue number
phofl Nov 9, 2020
07f13f3
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 9, 2020
adaa27b
Delete None
phofl Nov 9, 2020
061468c
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 10, 2020
d59e7f7
Change whatsnew
phofl Nov 12, 2020
d9562c4
Add comment
phofl Nov 12, 2020
c382af0
Improve test
phofl Nov 12, 2020
694e469
Change test description
phofl Nov 12, 2020
d78d8ce
Change issue number in test
phofl Nov 13, 2020
d9a4054
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 13, 2020
c13da44
Fix comments
phofl Nov 14, 2020
0a6cab7
Add testcases
phofl Nov 14, 2020
4ffe46e
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 14, 2020
3e9baa6
Fix bugs
phofl Nov 14, 2020
c579c88
Adress review comments
phofl Nov 15, 2020
0be97cd
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 15, 2020
923610e
Fix whatsnew
phofl Nov 16, 2020
fdd170e
Fix blank line and if else
phofl Nov 17, 2020
3f3c133
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 17, 2020
4c17aeb
Parametrize test
phofl Nov 17, 2020
a766f98
Run black
phofl Nov 17, 2020
48424fb
Merge branch 'master' of https://github.com/pandas-dev/pandas into 25165
phofl Nov 24, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ Indexing
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`MultiIndex` with a level named "0" (:issue:`37194`)
- Bug in :meth:`Series.__getitem__` when using an unsigned integer array as an indexer giving incorrect results or segfaulting instead of raising ``KeyError`` (:issue:`37218`)
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
- Bug in :meth:`DataFrame.loc` raised ``TypeError`` when non integer interval was given to select values from :class:`MultiIndex` (:issue:`25165`)

Missing
^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2702,7 +2702,10 @@ def _get_loc_single_level_index(self, level_index: Index, key: Hashable) -> int:
if is_scalar(key) and isna(key):
return -1
else:
return level_index.get_loc(key)
loc = level_index.get_loc(key)
# if isinstance(loc, slice):
Copy link
Contributor

Choose a reason for hiding this comment

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

this is commented?

Copy link
Member Author

Choose a reason for hiding this comment

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

Shit, no I tried something out. This has to get back in. Will add it back momentarily

Copy link
Member Author

Choose a reason for hiding this comment

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

Commented it back in. At least my test failed previously :)

# return loc.start
return loc

def get_loc(self, key, method=None):
"""
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,28 @@ def test_getitem_loc_commutability(multiindex_year_month_day_dataframe_random_da
result = ser[2000, 5]
expected = df.loc[2000, 5]["A"]
tm.assert_series_equal(result, expected)


def test_getitem_loc_datetime():
Copy link
Contributor

Choose a reason for hiding this comment

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

put partial in the name of the test, move to pandas/tests/indexing/test_partial.py

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

# GH: 25165
date_idx = pd.date_range("2019", periods=2, freq="MS")
df = DataFrame(
list(range(4)),
index=MultiIndex.from_product([date_idx, [0, 1]], names=["x", "y"]),
)
expected = DataFrame(
[2, 3],
index=MultiIndex.from_product(
[[pd.to_datetime("2019-02-01")], [0, 1]], names=["x", "y"]
),
)
result = df["2019-2":]
tm.assert_frame_equal(result, expected)
result = df.loc["2019-2":]
tm.assert_frame_equal(result, expected)

result = df.loc(axis=0)["2019-2":]
tm.assert_frame_equal(result, expected)

result = df.loc["2019-2":, :]
tm.assert_frame_equal(result, expected)