-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Clarify that MultiIndex.set_levels() interprets passed values as new components of the .levels attribute #28294
Comments
Those issues were about non-unique level names, yours about non-unique level values. And in this second case the story is simple: yes, they are and always were allowed. But I think you've misunderstood how In short, this function is probably not what you're looking for. And unfortunately, there are no obvious alternative to do what you want to do. |
Should we at least keep this open to improve the docs then? |
Makes sense! |
I could give a try at improving the docs for MultiIndex.set_levels(), if that's okay. |
@hweecat sure! |
rectify for failing tests DOC: added docs for MultiIndex.set_levels (pandas-dev#28294)
shorten lines to pass checks
shortened lines to pass checks
shortened lines to pass checks
I had a similar issue:
output: In pd.read_csv, you can not specify the dtype of the headers. Here I want the level 0 header to be of type int and not object. After reading this issue (#28294), I found that pandas interprets the new indexes as a mapping for unique values in the same order, therefore I tried this: |
Actually a better way is: |
Code Sample, a copy-pastable example if possible
Which raises a
ValueError
:Problem description
Despite a dataframe with non-unique MultiIndex can be created, they cannot be set using
set_levels()
. Is this behaviour expected? I believe non-unique level values were not allowed for a period of time (#18882), but then they were allowed again (#21423), so I am not sure which is the current convention.Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 0.25.1
numpy : 1.16.4
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.2
setuptools : 41.0.1
Cython : 0.29.13
pytest : 5.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.1.8
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.7
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.1.8
The text was updated successfully, but these errors were encountered: