-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Cannot remove_unused_categories
for CategoricalIndex in a MultiIndex
#30846
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
Hmm, thanks for the report @fujiisoup. There's been some changes to how MI levels are handled, but it doesn't seem to fix this issue. In this line,
The cc @topper-123. |
Thanks, @TomAugspurger, for your quick reply.
Yes, it would be nice.
|
IMO this works as intended as an ops on a single level is not intended to affect the whole This may not be very intuitive, but I'm not sure what to change. The workflow "Creating a new MultiIndex from an existing MultiIndex, but with some levels changed" is probably difficult to implement as a method. The best current approach is probably to extract the arrays to a DataFrame using I BTW have a branch where I'm trying to implement #27138, which would give us |
What do you think about a MultiIndex.remove_unused_categories? i.e. an
operation on the MultiIndex that affects all the (categorical) levels?
…On Thu, Jan 9, 2020 at 2:28 PM Terji Petersen ***@***.***> wrote:
IMO this works as intended as an ops on a single level is not intended to
affect the whole MultiIndex. If you want to create an MultiIndex with
unused categories removed, they currently need to be removed before
creating the MultiIndex.
This may not be very intuitive, but I'm not sure what to change. The
workflow "Creating a new MultiIndex from an existing MultiIndex, but with
some levels changed" is probably difficult to implement as a method. The
best approach is probably to extract the arrays to a DataFrame using
MultiIndex.to_frame, doing the needed changed on the DataFrame columns,
then use MultiIndex.from_frame to turn the DataFrame content into a
MultiIndex.
I have a branch where I'm trying to implement #27138
<#27138>, which would give us
MultiIndex.arrays to mirror Index.array etc. That would give us easy
access to the individual levels as Categoricals. But I am not making
progress ATM, so IDK when that will land.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30846?email_source=notifications&email_token=AAKAOISKDEDIBNIPL57C3XTQ46CITA5CNFSM4KEWYGUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIRVJ3Y#issuecomment-572740847>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAKAOIVU7EXZ4NUT4SY2CC3Q46CITANCNFSM4KEWYGUA>
.
|
I don't like it because it's too specific; There so many other level-changing methods that could be added that it seems arbitrary to add just that one IMO. Maybe if some more general could be found? |
Thanks all. full_idx = pd.MultiIndex.from_product(idx.levels, names=idx.names) and shape = tuple(lev.size for lev in idx.levels) only with used levels. Is there any good workaround for this? levels = []
for i, level in enumerate(index.levels):
if isinstance(level, pd.CategoricalIndex):
level = level[index.codes[i]].remove_unused_categories()
levels.append(level)
index = pd.MultiIndex.from_arrays(levels, names=index.names) which is not very beutiful. |
I'm working to add a
CategoricalIndex
support in xarray (pydata/xarray#3647, pydata/xarray#3670), but have a trouble.Code Sample, a copy-pastable example if possible
Problem description
When
CategoricalIndex
is a level variable of MultiIndex,remove_unused_categories
does not work.Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-1066-oem
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 42.0.2.post20191203
Cython : None
pytest : 5.3.2
hypothesis : 4.54.2
sphinx : 2.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.10.2
pandas_datareader: None
bs4 : None
bottleneck : 1.3.1
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.2
sqlalchemy : None
tables : None
xarray : 0.14.1
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
The text was updated successfully, but these errors were encountered: