-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ERR: disallow non-hashables in Index construction & rename #20527
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
Comments
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 |
I would like to work on this issue. |
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. |
Problem description
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:
Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 28.8.0
Cython: None
numpy: 1.14.1
scipy: None
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: