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: groupby.apply requires func to be hashable. raises unhashable type error otherwise #58345

Open
3 tasks done
furechan opened this issue Apr 21, 2024 · 2 comments
Open
3 tasks done
Labels
Apply Apply, Aggregate, Transform Bug Closing Candidate May be closeable, needs more eyeballs Groupby

Comments

@furechan
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
from dataclasses import dataclass

s = pd.DataFrame(dict(foo=list("XYYZ"), bar=list("ABCD"))).set_index("foo")

@dataclass
class Multiple:
    count: int = 2
    
    def __call__(self, x):
        return x * self.count

multiple = Multiple()
s.groupby("foo").apply(multiple)

Issue Description

In the previous example, groupby.apply is called with a callable dataclass instance, and the code raises the exception: TypeError: unhashable type: 'Multiple'.

Expected Behavior

For some reason pandas requires an hashable type here even though the argument is just used as a callable. Declaring the dataclass with unsafe_hash=True fixes the error, but it seems an unnecessary burden to always have to support __hash__ in this situation.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.14.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:19:22 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T8112
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 65.5.0
pip : 24.0
Cython : 3.0.10
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.2.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.4
numba : 0.59.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@furechan furechan added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 21, 2024
@Aloqeely
Copy link
Contributor

Thanks for the report! I am unable to reproduce this on main but I am able to reproduce on latest release 2.2.2
This seems to be caused by the following in pandas/core/groupby/groupby.py apply function:

orig_func = func
func = com.is_builtin_func(func)
if orig_func != func:
    alias = com._builtin_table_alias[orig_func]
    warn_alias_replacement(self, orig_func, alias)

Which internally was checking for whether or not the apply func you provided was deprecated or not by checking if it exists in the _builtin_table dictionary, and since dictionary keys must be hashable, _builtin_table.get(arg, arg) raised TypeError: unhashable type: 'Multiple'

I think this was fixed in #57444 which just enforced the deprecation and removed this check.
I would've called for a backport if this was fixed intentionally but I don't know what to do here. cc @rhshadrach

@rhshadrach
Copy link
Member

Thanks for investigating @Aloqeely - #57444 is changing long standing intentional behavior, so must be done in a major release.

We could patch this by doing the lookup as a try-except, since anytime it would fail we would want to use the function as-is anyways. But because this isn't a regression, I think we should just wait until 3.0 resolves it.

@rhshadrach rhshadrach added Groupby Apply Apply, Aggregate, Transform Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Closing Candidate May be closeable, needs more eyeballs Groupby
Projects
None yet
Development

No branches or pull requests

3 participants