-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
sort_index not sorting when multi-index made by different categorical types #24271
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
It seems like this might be the same issue. I have this MWE with pandas-0.23.4
Output:
With or without the |
Looks like this is fixed on master. Could use a test
|
Setting type then setting index gives expected output: dd = pd.DataFrame([
("five", "monday", "two", 1),
("four", "monday", "one", 1),
("one", "friday", "one", 1),
("one", "wednesday", "two", 1),
("three", "tuesday", "three", 1),
("two", "tuesday", "one", 1),
])
dd[0] = dd[0].astype(number_type)
dd[1] = dd[1].astype(day_type)
dd[2] = dd[2].astype(number_type)
dd.set_index([0,1,2]).sort_index()
Conversely, setting index then setting dtype gives the described "bug": dd = pd.DataFrame([
("five", "monday", "two", 1),
("four", "monday", "one", 1),
("one", "friday", "one", 1),
("one", "wednesday", "two", 1),
("three", "tuesday", "three", 1),
("two", "tuesday", "one", 1),
])
dd = dd.set_index([0, 1, 2])
dd.index = dd.index.set_levels(dd.index.levels[0].astype(number_type), 0)
dd.index = dd.index.set_levels(dd.index.levels[1].astype(day_type), 1)
dd.index = dd.index.set_levels(dd.index.levels[2].astype(number_type), 2)
This is because in the latter case, we only modify the levels but not the codes. Don't know if we have proper way for this usecase yet. Maybe it's in df.reindex but i couldnt work it out. |
Code Sample, a copy-pastable example if possible
This is the shorter code to reproduce the problem
Problem description
The dataframe is not sorted. I get
which is exactly what you get without any sorting.
If all the index levels have the same categorical type it seems to work.
It works if I reset the index:
Expected Output
Output of
pd.show_versions()
[paste the output of
pd.show_versions()
here below this line]INSTALLED VERSIONS
commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.16-300.fc29.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8
pandas: 0.23.4
pytest: 4.0.0
pip: 18.1
setuptools: 40.5.0
Cython: None
numpy: 1.15.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.1.1
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.6
feather: None
matplotlib: 3.0.1
openpyxl: None
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: None
lxml: None
bs4: 4.6.3
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: