-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
mi.drop(x).get_loc_level(x) returns empty slice (rather than raising KeyError) #22221
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
Can you try on master? This seemed to be working for me. |
Thanks for the quick response! Am I going to need to clone and build locally, or is the master on Conda or PIP? |
0.23.4 was just released so you can try upgrading to that and see if the issue is there (I have not personally), but if not you would clone and build |
I upgraded to 0.23.4 and had the same issue. However, it is fixed on master. |
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, |
Understood on the specific problem you are trying to solve but I'm not aware of any other places where this type of behavior is encouraged in the code-base, i.e. it may be non-idiomatic. @toobaz may have some thoughts on this as well |
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. |
Simpler way to reproduce:
|
Why was returning a KeyError here determined to be preferable to returning an empty DataFrame? |
Because the label is not in the index/level, and |
…pandas-dev#22230) * BUG: raise KeyError if MultiIndex.get_loc_level is asked unused label closes pandas-dev#22221 * TST: test groupby.apply() with user-defined function returning an empty chunk * CLN: remove named lambda
Code Sample
Problem description
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
KeyError: 'the label [empty] is not in the [index]'
(full output below). This is possible related to #21624.Despite what the error says, printing trimmed.indexes shows that 'empty' is a valid index:
Full Output:
Expected Output
Prior to 0.23, an empty Series would be returned.
0.22.0 output:
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.3
pytest: 3.6.4
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.15.0
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: 1.7.6
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.1
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: