-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: arithmetic can break equality-hash invariant for Timestamp during DST transition #60084
Comments
Thanks for the report! Agreed this would be a bug, I cannot reproduce on main but can on 2.3.x. Need to run a git bisect to see which PR fixed this and if tests need to be added. |
Thanks! I think this could use a test. Contributions welcome! |
is this issue resolved? |
@vgauraha62 - a test needs to be added in order for the issue to be resolved. |
I'll take a look at adding a test this weekend; I'll need to get up to speed on how things work around here, so others are welcome to pick this up if they have the bandwidth. @rhshadrach is there any additional action needed to correct this with the |
Not that I can see, but if you believe so, could you elaborate? |
Please assign this issue to me, I'd add a test in a couple of hours and get this resolved |
@rhshadrach sir are you suggesting that I should create a branch and submit a pull request? |
Hi @vgauraha62, you can comment |
take |
Apologies, I thought the initial test script replicated the issue but I have to explicitly use a import pandas as pd
import pytz
# this is immediately following a DST transition
dt_str = "2023-11-05 01:00-08:00"
tz_str = "America/Los_Angeles"
pytz_tz = pytz.timezone(tz_str)
ts1 = pd.Timestamp(dt_str, tz=pytz_tz)
ts2 = ts1 + pd.Timedelta(hours=0)
assert ts1 == ts2
assert hash(ts1) == hash(ts2)
print("hash-equality invariant upheld") |
are you still working on this issue?I'm also interested in this one . |
take |
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
Issue Description
Datetime arithmetic on a
Timestamp
instance on a DST transition can create twoTimestamps
that compare equal but have different hash values. Inspecting the example, the cause seems to be thatts1.fold == 0
andts2.fold == 1
, despite otherwise being equal and representing the same instant in time. This breaks the invariant documented inobject.__hash__
:Converting to UTC or using
pytz
's normalization asts.tz.normalize(ts)
both serve as workarounds for this issue.Expected Behavior
I expected
Timestamp
instances that compare equal to have the same hash value.Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.6
python-bits : 64
OS : Linux
OS-release : 5.15.153.1-microsoft-standard-WSL2
Version : #1 SMP Fri Mar 29 23:14:13 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8
pandas : 2.2.3
numpy : 2.1.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.1.2
Cython : None
sphinx : None
IPython : 8.28.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: