Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
ERR: disallow non-hashables in Index construction & rename #20527
from pandas import Index, DataFrame df = DataFrame(index=Index([1, 2, 3, 4]), columns=['A', 'B']) df.index.rename(['foo'], inplace=True) df.reset_index()
It think it is reasonable to expect Index([1, 2, 3, 4]).rename(['foo']) and Index([1, 2, 3, 4]).rename('foo') resulting in the same, however this is not the case.
Probably this line be modified in Index.rename:
>>>Index([1, 2, 3, 4]).rename(['foo']) Int64Index([1, 2, 3, 4], dtype='int64', name='foo')
The problem with your expected output is that some other 'containers', like a tuple, are actually allowed as an index name:
So since this is already allowed, we would need to keep this behaviour. And then unpacking a list but not a tuple would also be strange I think.
That said, I think if we choose to not change this behaviour, we should raise the error earlier, and
technically we do allow non-hashable things in index names. IIRC we did try to remove this in a previous PR (but was not merged). Note that we do require hashability in Series names. So would take a PR to raise (both on construction and renaming)
so will repurpose this issue
changed the title from
Calling rename() on Index object with a list makes reset_index() fail on a DataFrame
ERR: disallow non-hashables in Index construction & rename
Mar 29, 2018
referenced this issue
Mar 30, 2018
What if rename would be only modified as
def rename(self, *names, inplace=False): """...""" return self.set_names(names, inplace=inplace)
and remove the rename overwrite
class MultiIndex(Index): rename = Index.set_names # delete this
from MultiIndex. This way old functionality is kept (except MultiIndex.rename), but one can achive what I want by passing multiple
df = DataFrame(index=Index([1, 2, 3, 4]), columns=['A', 'B']) df.index.rename(['foo'], inplace=True) df.reset_index()
Still raises an error but
index.rename(*['foo']) index.rename(*['baz', 'quz'])
can be called on either Index and MultiIndex objects. So that in cases like mine, where it is not known beforehand whether I will be dealing with a multi or single level index, I can avoid the typecheck and rename the levels easily and then reset the index on the dataframe.
Please note that I did not have time to check te corner cases or run a regression on this idea, I am only interested in your opinion.