Skip to content
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

Wrong index type from groupby on an empty dataframe using user-defined agg func #14538

Closed
simonbrownsb opened this issue Oct 29, 2016 · 1 comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Groupby

Comments

@simonbrownsb
Copy link

A small, complete example of the issue

import pandas as pd

df = pd.DataFrame(columns=['a', 'b'])
df2 = df.groupby('a').agg({
    'a': {'max_a': lambda x: x.max()},
    'b': {'count_b': 'count'}
})
print(df2.index)

Expected Output

Index([], dtype='object', name='a') 

(This is the sort of index you would get if it wasn't an empty dataframe, or if the aggregation function was a cythoned builtin string like 'max').

Actual Output

RangeIndex(start=0, stop=0, step=1)

Nondeterministic in Python3 (sometimes the expected result, sometimes not, with the same code, on different runs).

Deterministically wrong in Python2 (provided the outer agg dictionary is ordered so that the user-defined function agg appears first in the list of keys).

Output of pd.show_versions()

## INSTALLED VERSIONS

commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Darwin
OS-release: 15.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.18.1
nose: None
pip: 6.0.8
setuptools: 12.2
Cython: 0.24.1
numpy: 1.11.1
scipy: None
statsmodels: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.7.3
boto: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Oct 29, 2016

already fixed in 0.19.0, could have been related to #12824 or #12821 but lots of fixes

In [1]: pd.__version__
Out[1]: '0.19.0'

In [2]: df = pd.DataFrame(columns=['a', 'b'])
   ...: df2 = df.groupby('a').agg({
   ...:     'a': {'max_a': lambda x: x.max()},
   ...:     'b': {'count_b': 'count'}
   ...: })
   ...: print(df2.index)
   ...: 
Index([], dtype='object', name='a')

@jreback jreback closed this as completed Oct 29, 2016
@jreback jreback added Groupby Dtype Conversions Unexpected or buggy dtype conversions labels Oct 29, 2016
@jreback jreback added this to the No action milestone Oct 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Groupby
Projects
None yet
Development

No branches or pull requests

2 participants