Description
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