mi.drop(x).get_loc_level(x) returns empty slice (rather than raising KeyError) #22221
import pandas as pd df = pd.DataFrame(dict(value=[0, 1], group=['filled','empty'])) groups = df.groupby('group') def remove1 (group): return group[group.value != 1]['value'] trimmed = groups.apply(remove1) print("Trimmed.loc['empty']:\n",trimmed.loc['empty'])
Since Pandas 0.23, if a group (ex. 'empty') is emptied by GroupBy.apply(), then accessing the result (ex. trimmed) through trimmed.loc['empty'] returns the error
Despite what the error says, printing trimmed.indexes shows that 'empty' is a valid index:
Prior to 0.23, an empty Series would be returned.
Thanks for confirming. Good to know its "fixed," though I'm actually not sure if
@jreback any thoughts?
In the original program where I encountered the error,
I'm afraid the fact that this works on master is the actual bug. This is related to #19027 .
@HuntJSparra I can understand what you have in mind, but you are simply looking in an index for a value which is not there, and this would only work in the past - and works again in master - because of an implementation glitch. To be honest I don't even see the try-catch as the right way to go: since some groups just don't have outliers, you might want to loop over
In any case, we must understand what happened in master that made this work again.
referenced this issue
Aug 7, 2018
Because the label is not in the index/level, and