Skip to content

Fixes: [#60084] Added a new file test_timestamp_hash.py #60115

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/scalar/test_timestamp_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
///
layout: pandas-dev
title: pandas
author: vgauraha62
///

import pandas as pd
import pytest
import logging

# Starting with logging configuration, to get the insights on the console, important!!
logging.basicConfig(level=logging.INFO)

def test_timestamp_hash_equality_on_dst():
logging.info("Testing timestamp hash equality on Daylight Saving Time (DST) transition")

# This is a DDaylight Saving Time (DST) transition for the America/Los_Angeles time zone
dt_str = "2023-11-05 01:00-08:00"
tz_str = "America/Los_Angeles"

ts1 = pd.Timestamp(dt_str, tz=tz_str)
logging.info(f"Created timestamp ts1: {ts1}")

ts2 = ts1 + pd.Timedelta(hours=0) # This here, should create the same timestamp
logging.info(f"Created timestamp ts2: {ts2}")

# Now we verify that the timestamps compare equal
assert ts1 == ts2, "Timestamps are not considered equal when they should be."
logging.info("Timestamps are equal")

# Important to Convert to UTC before comparing hash values to avoid Daylight Saving Time (DST) issues
ts1_utc = ts1.tz_convert('UTC')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting to UTC before comparing the hashes doesn't replicate the behavior I reported--the bug is one of those "DST issues" that UTC avoids 😄. The reported issue is that the hashes of the original ts1 and ts2 instances, without normalization, don't have the same hash though they compare equal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i read through it again. maybe i should stop normalizing timestamps to UTC, and start comparing the hash values in their original timezones.

logging.info(f"Converted ts1 to UTC: {ts1_utc}")

ts2_utc = ts2.tz_convert('UTC')
logging.info(f"Converted ts2 to UTC: {ts2_utc}")

# Finally we erify that their hash values are the same, to successfully achieve testing
assert hash(ts1_utc) == hash(ts2_utc), "Hashes of equal Timestamps do not match after normalization."
logging.info("Hashes of timestamps are equal")
Loading