-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ERR: disallow non-hashables in Index/MultiIndex construction & rename #20548
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
Changes from 1 commit
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,10 +252,6 @@ 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('{}.name must be a hashable type' | ||
.format(cls.__name__)) | ||
|
||
if fastpath: | ||
return cls._simple_new(data, name) | ||
|
||
|
@@ -478,7 +474,7 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs): | |
|
||
result = object.__new__(cls) | ||
result._data = values | ||
result.name = name | ||
result._set_names([name]) | ||
for k, v in compat.iteritems(kwargs): | ||
setattr(result, k, v) | ||
return result._reset_identity() | ||
|
@@ -1316,6 +1312,45 @@ def _get_names(self): | |
return FrozenList((self.name, )) | ||
|
||
def _set_names(self, values, level=None): | ||
""" | ||
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. can you also add a mention on |
||
Set new names on index. | ||
|
||
Parameters | ||
---------- | ||
values : str or sequence | ||
name(s) to set | ||
level : int, level name, or sequence of int/level names (default None) | ||
If the index is a MultiIndex (hierarchical), level(s) to set (None | ||
for all levels). Otherwise level must be None | ||
|
||
Returns | ||
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 should be Raises (and its a TypeError) |
||
------- | ||
index : Index | ||
|
||
See Also | ||
-------- | ||
Index.set_names : Set new names on index. Defaults to returning | ||
new index. | ||
Index.rename : Set new names on index. Defaults to returning new index. | ||
|
||
Notes | ||
----- | ||
Both `set_names` and `rename` call this function. | ||
|
||
Examples | ||
-------- | ||
on an index with no 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. you don't need the fulll doc string here (e.g. examples and such, leave Parameters and such), only on |
||
>>> I1 = Index([1, 2, 3, 4]) | ||
>>> I1._set_names([0]) | ||
>>> I1 | ||
Int64Index([1, 2, 3, 4], dtype='int64', name=0) | ||
|
||
set multiple names: | ||
>>> I2 = Index([1, 2, 3, 4], name="foo") | ||
>>> I2._set_names([(0, 1)]) | ||
>>> I2 | ||
Int64Index([1, 2, 3, 4], dtype='int64', name=(0, 1)) | ||
""" | ||
# GH 20527 | ||
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. can you add a doc-string here |
||
# All items in 'name' need to be hashable: | ||
if values is not None: | ||
|
@@ -1351,9 +1386,9 @@ def set_names(self, names, level=None, inplace=False): | |
Examples | ||
-------- | ||
>>> Index([1, 2, 3, 4]).set_names('foo') | ||
Int64Index([1, 2, 3, 4], dtype='int64') | ||
Int64Index([1, 2, 3, 4], dtype='int64', name='foo') | ||
>>> Index([1, 2, 3, 4]).set_names(['foo']) | ||
Int64Index([1, 2, 3, 4], dtype='int64') | ||
Int64Index([1, 2, 3, 4], dtype='int64', name='foo') | ||
>>> idx = MultiIndex.from_tuples([(1, u'one'), (1, u'two'), | ||
(2, u'one'), (2, u'two')], | ||
names=['foo', 'bar']) | ||
|
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.
@jreback I wasn't sure if
_set_names
was getting called from_simple_new
, so I made it explicit. Is this ok?Also, we are not checking in
__new__
anymore (as you suggested).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.
you shouldn't need to do this, and just leave the original code
setting
.name
name is a property that calls_set_names