-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ERR: disallow non-hashables in Index/MultiIndex construction & rename #20548
Changes from 4 commits
9047d60
df7650d
dd64219
89e92ab
cd3e53a
cd070e3
351691f
3a7b0b2
70933d5
56fd617
d4ed636
b554bb3
6efd6cc
4fb3a6b
786f43f
01b712e
6f13cd0
26433c3
91ef466
85e35ea
5c2e240
4ca2a52
840cd88
18bcf2a
d98014f
b8a1d7e
edfbd1d
2322346
fa52655
c0f6936
a9c14e6
30da596
667d495
c4c1011
bd75433
74a9b54
b1cb7fd
863f7d3
0723009
7092d49
1d8f67a
12488ff
4a500ba
9ec64b0
47903ae
04f2eed
1a68188
97a2b06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
is_datetime64_any_dtype, | ||
is_datetime64tz_dtype, | ||
is_timedelta64_dtype, | ||
is_hashable, | ||
needs_i8_conversion, | ||
is_iterator, is_list_like, | ||
is_scalar) | ||
|
@@ -251,6 +252,9 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, | |
if name is None and hasattr(data, 'name'): | ||
name = data.name | ||
|
||
if name is not None and not is_hashable(name): | ||
raise TypeError('Index.name must be a hashable type') | ||
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. use |
||
|
||
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. this very likely also needs checking for MultiIndex (as that's a different path in some cases). 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. Do we allow non-hashable names for MultiIndex? 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. no 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. For a MultiIndex, it seems that Right now, if
while this passes:
Do we need to change anything here? 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. no you just need to check that each name is hashable, not the frozen list itself. that's why .set_names is the best place for this |
||
if fastpath: | ||
return cls._simple_new(data, name) | ||
|
||
|
@@ -1392,7 +1396,10 @@ def rename(self, name, inplace=False): | |
------- | ||
new index (of same type and class...etc) [if inplace, returns None] | ||
""" | ||
return self.set_names([name], inplace=inplace) | ||
if name is not None and not is_hashable(name): | ||
raise TypeError('Index.name must be a hashable type') | ||
else: | ||
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. rather do this in set_names 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. I did this in set_names and a lot of tests failed. Is there a particular reason we can’t keep it here? 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. you are going to need it in set_names as that is the canonical way to do this. that's where it should validate. if we have tests that are clearly in error they should be changed. |
||
return self.set_names([name], inplace=inplace) | ||
|
||
@property | ||
def _has_complex_internals(self): | ||
|
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.
I don't actually think you need this here, as
_set_name
gets called from_simple_new
(when.name
is set). so rather more logical to put this in_set_name
(like you do for MI)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.
For MI, the name validation is delegated to
_set_names
in this way:pandas/pandas/core/indexes/multi.py
Lines 223 to 231 in 2794474
Maybe add something like this in
__new__()
:So that
pd.Index([1, 2, 3], name=['foo'])
would still raise but we check in a more logical place.