-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
GroupBy Not Throwing KeyError When Names Exist in MultiIndex #25704
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
You should get the desired behavior by selecting levels: In [11]: data['N'].groupby(level=['date', 'name']).sum()
Out[11]:
date name
2016-02-15 A 5
Name: N, dtype: int64 This should be raising a PRs to make that happen would certainly be welcome |
@WillAyd According to Grouping DataFrame with Index Levels and Columns : "Index level names may be specified as keys directly to groupby" (starting with version 0.20, see In/Out[51] on the page). So, what I did should be correct. Am I misunderstanding anything? |
I think, it has something to do with the following selection line in my example: data = data[ data.index.get_level_values('time')>=pd.to_timedelta('09:30:00') ] Without it, the code works correctly. |
Or it might have something to do with the import pandas as pd
data = pd.DataFrame(data={'date':[pd.Timestamp('2016-02-15')]*2,
'time':[pd.Timedelta(x) for x in ('10:10:00','11:10:00')],'name':['A']*2,
'N':[2,3]}).set_index(['date','time','name']).sort_index()
dataGB = data['N'].groupby(['date','name'])
print(data)
print('Number of groups:',len(dataGB))
print(dataGB.sum())
print(pd.__version__) |
Hmm OK thanks for sharing that. I see this was actually implemented in #14432 Looking at the test coverage there I don't see anything that has multiple index levels without a column selection, so that may be the culprit here. Investigation and PRs would be welcome |
I can take this one, I see where the problem is. It appears to be in |
Here is a link to the discussion: pandas Series groupby with one group
Code Sample, a copy-pastable example if possible
Problem description
Real Output
Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-862.11.6.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.utf-8
LANG: en_US.utf-8
LOCALE: en_US.UTF-8
pandas: 0.24.1
pytest: 3.3.2
pip: 19.0.3
setuptools: 39.0.1
Cython: 0.27.3
numpy: 1.16.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: