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

BUG: Series.groupby() fails in pandas 1.1.4 when index has tuple name. #37755

Closed
2 of 3 tasks
burk opened this issue Nov 11, 2020 · 1 comment · Fixed by #37801
Closed
2 of 3 tasks

BUG: Series.groupby() fails in pandas 1.1.4 when index has tuple name. #37755

burk opened this issue Nov 11, 2020 · 1 comment · Fixed by #37801
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version Series Series data structure
Milestone

Comments

@burk
Copy link

burk commented Nov 11, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

print("Test 1")
a = pd.Series([1,2,3,4], index=[1,1,2,2], name=("a", "a"))
a.index.name = ("b", "b")
print(a)
print(a.index)
print(a.groupby(level=0).last())

print("Test 2")
a = pd.Series([1,2,3,4], index=[2,3,4,5], name=("a", "a"))
b = pd.Series([1,1,2,2], index=[2,3,4,5], name=("b", "b"))
a.index = b.reindex(a.index)
print(a)
print(a.index)
print(a.groupby(level=0).last())

Problem description

In pandas 1.1.2 this works fine. While it crashes in pandas 1.1.4. The problem seems related to the tuple index names. The output is:

Test 1
(b, b)
1    1
1    2
2    3
2    4
Name: (a, a), dtype: int64
Int64Index([1, 1, 2, 2], dtype='int64', name=('b', 'b'))
Traceback (most recent call last):
  File "testcase.py", line 8, in <module>
    print(a.groupby(level=0).last())
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/series.py", line 1735, in groupby
    return SeriesGroupBy(
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 525, in __init__
    grouper, exclusions, obj = get_grouper(
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 773, in get_grouper
    if is_in_obj(gpr):  # df.groupby(df['name'])
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 765, in is_in_obj
    return gpr is obj[gpr.name]
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/series.py", line 888, in __getitem__
    result = self._get_value(key)
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/series.py", line 989, in _get_value
    loc = self.index.get_loc(label)
  File "/home/burk/.local/share/virtualenvs/timeseries-Gj3kjmJv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2895, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 96, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 118, in pandas._libs.index.IndexEngine._get_loc_duplicates
TypeError: only integer scalar arrays can be converted to a scalar index

Expected Output

This is the output in 1.1.2:

Test 1
(b, b)
1    1
1    2
2    3
2    4
Name: (a, a), dtype: int64
Int64Index([1, 1, 2, 2], dtype='int64', name=('b', 'b'))
(b, b)
1    2
2    4
Name: (a, a), dtype: int64

Test 2
(b, b)
1    1
1    2
2    3
2    4
Name: (a, a), dtype: int64
Int64Index([1, 1, 2, 2], dtype='int64', name=('b', 'b'))
(b, b)
1    2
2    4
Name: (a, a), dtype: int64

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-26-generic
Version : #27-Ubuntu SMP Wed Oct 21 22:29:16 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.4
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.1.1
setuptools : 44.0.0
Cython : 0.29.17
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.6.1
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : None
IPython : 7.19.0
pandas_datareader: 0.9.0
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 2.0.0
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.20
tables : None
tabulate : 0.8.7
xarray : None
xlrd : None
xlwt : None
numba : 0.51.2

@burk burk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 11, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Nov 11, 2020
@simonjayhawkins
Copy link
Member

Thanks @burk for the report.

first bad commit: [2000334] Backport PR #36147: REGR: Series access with Index of tuples/frozenset (#36332) cc @rhshadrach

@simonjayhawkins simonjayhawkins added Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 12, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.5 milestone Nov 12, 2020
@rhshadrach rhshadrach self-assigned this Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version Series Series data structure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants