-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: raise on non-hashable Index name, closes #29069 #30335
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
75ef96f
BUG: raise on non-hashable Index name, closes #29069
jbrockmendel bac89e5
no fstring
jbrockmendel eb7a980
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 251a1a8
make name a property with setter
jbrockmendel c1694ef
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel c72fd0b
mypy fixup
jbrockmendel 647966e
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 695ee03
Address comments
jbrockmendel 76407cf
typo fixup
jbrockmendel 880c092
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from datetime import datetime | ||
import operator | ||
from textwrap import dedent | ||
from typing import FrozenSet, Union | ||
from typing import FrozenSet, Hashable, Optional, Union | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -239,7 +239,7 @@ def _outer_indexer(self, left, right): | |
_typ = "index" | ||
_data: Union[ExtensionArray, np.ndarray] | ||
_id = None | ||
name = None | ||
_name: Optional[Hashable] = None | ||
_comparables = ["name"] | ||
_attributes = ["name"] | ||
_is_numeric_dtype = False | ||
|
@@ -274,8 +274,7 @@ def __new__( | |
from .interval import IntervalIndex | ||
from .category import CategoricalIndex | ||
|
||
if name is None and hasattr(data, "name"): | ||
name = data.name | ||
name = maybe_extract_name(name, data, cls) | ||
|
||
if isinstance(data, ABCPandasArray): | ||
# ensure users don't accidentally put a PandasArray in an index. | ||
|
@@ -520,7 +519,7 @@ def _simple_new(cls, values, name=None, dtype=None): | |
# data buffers and strides. We don't re-use `_ndarray_values`, since | ||
# we actually set this value too. | ||
result._index_data = values | ||
result.name = name | ||
result._name = name | ||
|
||
return result._reset_identity() | ||
|
||
|
@@ -1209,6 +1208,15 @@ def to_frame(self, index=True, name=None): | |
# -------------------------------------------------------------------- | ||
# Name-Centric Methods | ||
|
||
@property | ||
def name(self): | ||
return self._name | ||
|
||
@name.setter | ||
def name(self, value): | ||
maybe_extract_name(value, None, type(self)) | ||
self._name = value | ||
|
||
def _validate_names(self, name=None, names=None, deep=False): | ||
""" | ||
Handles the quirks of having a singular 'name' parameter for general | ||
|
@@ -1258,7 +1266,7 @@ def _set_names(self, values, level=None): | |
for name in values: | ||
if not is_hashable(name): | ||
raise TypeError(f"{type(self).__name__}.name must be a hashable type") | ||
self.name = values[0] | ||
self._name = values[0] | ||
|
||
names = property(fset=_set_names, fget=_get_names) | ||
|
||
|
@@ -1546,7 +1554,7 @@ def droplevel(self, level=0): | |
if mask.any(): | ||
result = result.putmask(mask, np.nan) | ||
|
||
result.name = new_names[0] | ||
result._name = new_names[0] | ||
return result | ||
else: | ||
from .multi import MultiIndex | ||
|
@@ -1777,7 +1785,7 @@ def __setstate__(self, state): | |
nd_state, own_state = state | ||
data = np.empty(nd_state[1], dtype=nd_state[2]) | ||
np.ndarray.__setstate__(data, nd_state) | ||
self.name = own_state[0] | ||
self._name = own_state[0] | ||
|
||
else: # pragma: no cover | ||
data = np.empty(state) | ||
|
@@ -5462,3 +5470,19 @@ def default_index(n): | |
from pandas.core.indexes.range import RangeIndex | ||
|
||
return RangeIndex(0, n, name=None) | ||
|
||
|
||
def maybe_extract_name(name, obj, cls) -> Optional[Hashable]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my comment above, can be more general so need to relocate i think as this is common between series/index) |
||
""" | ||
If no name is passed, then extract it from data, validating hashability. | ||
""" | ||
if name is None and isinstance(obj, (Index, ABCSeries)): | ||
# Note we don't just check for "name" attribute since that would | ||
# pick up e.g. dtype.name | ||
name = obj.name | ||
|
||
# GH#29069 | ||
if not is_hashable(name): | ||
raise TypeError(f"{cls.__name__}.name must be a hashable type") | ||
|
||
return name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in series.py you can change name.setter to use maybe_extract_name (ok for followon)