-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pd.Timedelta breaks hash invariant #44504
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
Comments
@harahu Thanks for the report. The problem is that pd.Timedelta can also evaluate equal with the standard library pd_0_dt = pd.Timedelta(0)
np_0_dt = np.timedelta64(0)
stdlib_0_dt= datetime.timedelta(0) we get:
So the hash invariance holds between (in fact in this case the |
I understand that is is a desirable property to have both of Having import pandas as pd
s = pd.Series([pd.Timedelta(value=i, unit="D") for i in range(5)])
uniques = s.unique()
reprs = dict(zip(uniques, (repr(u) for u in uniques)))
reprs_keys = list(reprs.keys())
for td in s:
assert td in reprs_keys
# Assuming all is good and I have no worries at this point
# Say hello to unexpected KeyError: Timedelta('0 days 00:00:00')
print(reprs[td]) Although this example might seem contrived, that is only the case because it is, as mentioned, simplified. When objects get processed in complex library code, sometimes out of your control, it is just a question of time before the lack of hash invariance leads to subtle or not-so-subtle errors. Looking at the example above, the only reasonable outcomes are one of:
|
Would the particular example with s.unique be solved by #42741? Some observations:
|
Seems that way. If I understand it correctly, one would then have the members of |
xref numpy/numpy#3836 |
Good find! |
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 master branch of pandas.
Reproducible Example
Issue Description
Ref: https://bugs.python.org/issue45832
I was prompted by @rhettinger to bring this up with you.
pd.Timedelta
andnp.timedelta64
are in violation of the python hash invariant.Specifically:
Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 945c9ed
python : 3.10.0.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.3.4
numpy : 1.21.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.3.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.29.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 6.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: