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

Behaviour of comparisons between pandas.Timestamp and datetime.date objects changes in Pandas v2.0 #934

Closed
matt-graham opened this issue Apr 5, 2023 · 2 comments · Fixed by #937
Labels
bug Something isn't working

Comments

@matt-graham
Copy link
Collaborator

Pandas v2.0 changes the behaviour of equality comparisons of pandas.Timestamp and datetime.date objects to always compare as unequal (pandas-dev/pandas#36131). For example

import datetime, pandas
print(pandas.Timestamp(2010, 1, 1) == datetime.date(2010, 1, 1))

outputs True in Pandas v1.2.2 and False in v2.0.0.

This causes the assert in tests/test_control_order_of_events.py::test_control_of_ordering_in_the_day

events['date'] = pd.to_datetime(events['date']).dt.date
order_on_day_one = tuple(events.loc[events['date'] == Date(2010, 1, 1), 'id'])
assert order_on_day_one == ("EventForStartOfDay",
"EventForMiddleOfDay",
"EventForSecondToLastAtEndOfDay",
"EventForEndOfDay")

to fail when running with Pandas v2.0.0 as the earlier comparison with a Date object (corresponding to a pandas.Timestamp) no longer behaves as expected.

@matt-graham
Copy link
Collaborator Author

Found another instance of this causing a problem in tests/test_healthcareseeking.py::test_force_healthcare_seeking

evs = [x[1].TREATMENT_ID for x in
sim.modules['HealthSystem'].find_events_for_person(0) if x[0].date() == start_date]

The x[0] tuple entry here is a pd.Timestamp object already so just removing the .date() call which convert is to a datetime.date object appears to fix things. @tbhallett can you remember why you added this conversion in this first place?

@tbhallett
Copy link
Collaborator

Found another instance of this causing a problem in tests/test_healthcareseeking.py::test_force_healthcare_seeking

evs = [x[1].TREATMENT_ID for x in
sim.modules['HealthSystem'].find_events_for_person(0) if x[0].date() == start_date]

The x[0] tuple entry here is a pd.Timestamp object already so just removing the .date() call which convert is to a datetime.date object appears to fix things. @tbhallett can you remember why you added this conversion in this first place?

I can't! But the changes seem good to me. Thanks Matt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants