groupby indexing is giving the wrong index · Issue #28652 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

a = pd.DataFrame([range(3)]*3,index=map(str, range(3))).T a.iloc[:,0] = [10, 11, 11]

a.groupby('0').apply(lambda x: print(x.index))

indexes = a.groupby('0').apply(lambda x: x.index) print(indexes)

index_lengths = a.groupby('0').apply(lambda x: len(x.index)) print(index_lengths)

indexes_lengths = a.groupby('0').apply(lambda x: x.index).apply(len) print(indexes_lengths)

assert indexes_lengths.equals(index_lengths), "Apply indexes are returning the wrong index"

Problem description

Groupby indexing is returning the wrong value. It is returning the indexes of the last group for all the groups.

Expected Output

0
10    Int64Index([0], dtype='int64')
11    Int64Index([1, 2], dtype='int64')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.2.0-arch2-1-ARCH
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C
LOCALE : None.None

pandas : 0.25.1
numpy : 1.15.4
pytz : 2019.1
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : 0.28.5
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.0
xlsxwriter : None
lxml.etree : 4.3.4
html5lib : None
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.5.0
pandas_datareader: None
bs4 : None
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.3.4
matplotlib : 2.2.2
numexpr : 2.6.9
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.13.0
pytables : None
s3fs : 0.1.5
scipy : 1.3.1
sqlalchemy : None
tables : 3.4.4
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None