Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
2 changes: 2 additions & 0 deletions pandas/_libs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = [
"NA",
"Interval",
"NaT",
"NaTType",
Expand All @@ -16,6 +17,7 @@
import pandas._libs.pandas_parser # isort: skip # type: ignore[reportUnusedImport]
import pandas._libs.pandas_datetime # noqa: F401 # isort: skip # type: ignore[reportUnusedImport]
from pandas._libs.interval import Interval
from pandas._libs.missing import NA
Copy link
Member

Choose a reason for hiding this comment

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

seems this change is unrelated to the rest of the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, actually, I was trying something and forgot to undo this change. It's fixed in the latest commit.

from pandas._libs.tslibs import (
NaT,
NaTType,
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,10 @@ def _validate_index_level(self, level) -> None:
verification must be done like in MultiIndex.

"""
if isinstance(level, int):
if type(level) is int:
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a simple edge case where the Index.name is also an int

Suggested change
if type(level) is int:
if type(level) is int:
if isinstance(self.name, int) and level == self.name:
return

Copy link
Member

Choose a reason for hiding this comment

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

lib.is_integer

if isinstance(self.name, int) and level == self.name:
return

if level < 0 and level != -1:
raise IndexError(
"Too many levels: Index has only 1 level, "
Expand All @@ -2094,7 +2097,10 @@ def _validate_index_level(self, level) -> None:
raise IndexError(
f"Too many levels: Index has only 1 level, not {level + 1}"
)
elif level != self.name:

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this is a better verification that validates what is expected from the documentation.

Suggested change
elif isinstance(level, str) and isinstance(self.name, str) and level != self.name:
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)

elif (
isinstance(level, str) and isinstance(self.name, str) and level != self.name
):
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)
Expand Down
Loading