Skip to content

Groupby on single key should be accessible by tuple of length 1 #25971

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

Closed
KeithWM opened this issue Apr 3, 2019 · 2 comments · Fixed by #54155
Closed

Groupby on single key should be accessible by tuple of length 1 #25971

KeithWM opened this issue Apr 3, 2019 · 2 comments · Fixed by #54155
Assignees
Labels
Bug Deprecate Functionality to remove in pandas Groupby
Milestone

Comments

@KeithWM
Copy link

KeithWM commented Apr 3, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4], 'col3': [5, 6]})


def select_by_dict(dataframe, d):
    return dataframe.groupby(list(d.keys())).get_group(tuple(d.values()))


select_by_dict(df, {'col1': 1, 'col2': 3})
select_by_dict(df, {'col1': 1})  # this one fails

Problem description

I would like to use a dictionary of arbitrary length to select certain groups from a DataFrame, but if I group by a single key, it won't accept a tuple of length 1 as the key value.

Expected Output

I expect the second call to the function to return a DataFrame containing those values for which col1 == 1. (I.e. as df[df['col1'] == 1]). Perhaps my approach is ill-advised, but I have come across this problem before in situations where it was more obvious to use groupby (in the current case it might well be better to use boolean indexing).

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Darwin OS-release: 18.2.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.23.3 pytest: 3.3.1 pip: 18.1 setuptools: 36.6.0 Cython: 0.29.5 numpy: 1.14.5 scipy: 1.1.0 pyarrow: None xarray: None IPython: 5.4.0 sphinx: None patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: None tables: 3.4.2 numexpr: 2.6.4 feather: None matplotlib: 2.0.2 openpyxl: 2.5.4 xlrd: 1.1.0 xlwt: None xlsxwriter: None lxml: None bs4: 4.7.1 html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

I hope you find this a useful issue.

@WillAyd
Copy link
Member

WillAyd commented Apr 3, 2019 via email

@KeithWM
Copy link
Author

KeithWM commented Apr 4, 2019

I did do a search for similar issues, naturally, but did not find anything. I will have a more thorough search and let you know whether I find anything.

@WillAyd WillAyd added the Groupby label Apr 9, 2019
@mroeschke mroeschke added the Bug label Jul 2, 2021
@rhshadrach rhshadrach self-assigned this Jul 16, 2023
@rhshadrach rhshadrach added the Deprecate Functionality to remove in pandas label Jul 16, 2023
@rhshadrach rhshadrach added this to the 2.1 milestone Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Deprecate Functionality to remove in pandas Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants