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

BUG: Date objects cannot be compared against a DatetimeIndex #35466

Open
2 of 3 tasks
knabben opened this issue Jul 29, 2020 · 8 comments
Open
2 of 3 tasks

BUG: Date objects cannot be compared against a DatetimeIndex #35466

knabben opened this issue Jul 29, 2020 · 8 comments
Labels
Bug datetime.date stdlib datetime.date support Datetime Datetime data dtype Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version

Comments

@knabben
Copy link
Contributor

knabben commented Jul 29, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

Version 1.0.5

holidays = USFederalHolidayCalendar().holidays()
>>> datetime.datetime(2018, 7, 4) in holidays
True
>>> datetime.date(2018, 7, 4) in holidays
True
>>> pd.__version__
'1.0.5'

Version 1.1.0

holidays = USFederalHolidayCalendar().holidays()
>>> datetime.datetime(2018, 7, 4) in holidays
True
>>> datetime.date(2018, 7, 4) in holidays
False
>>> pd.__version__
'1.1.0'
>>>

Problem description

On version 1.0.5 it was possible to compare via contains a datetime.date object against a DatetimeIndex, this behavior is not true anymore for the 1.1.0, is this an expected behavior to allow only recognized scalars objects?

Expected Output

Allow dates to be compared with 00:00:00 time.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.6
OS : Darwin

pandas : 1.1.0
numpy : 1.19.1
Cython : None

@knabben knabben added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 29, 2020
@simonjayhawkins
Copy link
Member

Thanks @knabben for the report

On version 1.0.5 it was possible to compare via contains a datetime.date object against a DatetimeIndex, this behavior is not true anymore for the 1.1.0, is this an expected behavior to allow only recognized scalars objects?

#31023 is the cause of this regression, so doesn't appear to be an intentional change cc @jbrockmendel

9b0ef5d is the first bad commit
commit 9b0ef5d
Author: jbrockmendel [email protected]
Date: Tue Jan 14 19:10:24 2020 -0800

refactor DTI.get_loc (#31023)

@simonjayhawkins simonjayhawkins added Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 30, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.1 milestone Jul 30, 2020
@jreback jreback modified the milestones: 1.1.1, 1.2 Aug 14, 2020
@jreback
Copy link
Contributor

jreback commented Aug 14, 2020

this needs more discussion

@dwardzinski-jsf
Copy link

dwardzinski-jsf commented Aug 21, 2020

I hope this gets more attention. This also broke DataFrame/Series indexing:

>>> date = datetime.date(2000, 1, 1)
>>> s = pd.Series([1], index=pd.to_datetime([date]))
>>> s.loc['2000-01-01']
1
>>> s.loc[date]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dwardzinski/.local/lib/python3.8/site-packages/pandas/core/indexing.py", line 879, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "/home/dwardzinski/.local/lib/python3.8/site-packages/pandas/core/indexing.py", line 1110, in _getitem_axis
    return self._get_label(key, axis=axis)
  File "/home/dwardzinski/.local/lib/python3.8/site-packages/pandas/core/indexing.py", line 1059, in _get_label
    return self.obj.xs(label, axis=axis)
  File "/home/dwardzinski/.local/lib/python3.8/site-packages/pandas/core/generic.py", line 3482, in xs
    loc = self.index.get_loc(key)
  File "/home/dwardzinski/.local/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py", line 622, in get_loc
    raise KeyError(key)
KeyError: datetime.date(2000, 1, 1)

This broke a lot of stuff for me, since my software relies pretty heavily on indexing with date objects.
Also, #35478 (@knabben's PR) doesn't appear to fix this issue unfortunately.

@jreback
Copy link
Contributor

jreback commented Nov 28, 2020

there is not a PR to actually change this, nor has this been discussed, moving off 1.2

@jbrockmendel
Copy link
Member

The current behavior of DatetimeIndex refusing to compare to a date object is correct, and we need to change the Timestamp behavior to match it #36131 along with the stdlib.

@simonjayhawkins
Copy link
Member

The current behavior of DatetimeIndex refusing to compare to a date object is correct

should this behaviour be deprecated first?

and for the indexing case, #35466 (comment) (maybe need to create separate issue), is KeyError appropriate or would TypeError now be more appropriate.

to be consistent with Python list indexing...

>>> [1,2,3]["a"]
<stdin>:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
>>>

perhaps something like TypeError: DatetimeIndex indices must be a time-like string, pd.Timestamp, datetime.datetime or an array of those or slices, not datetime.date

@jbrockmendel
Copy link
Member

KeyError appropriate or would TypeError now be more appropriate.

In 1.1.0 we made it so that failed label-based lookups always raise KeyError (see _whatsnew_110.notable_bug_fixes.indexing_raises_key_errors)

@jbrockmendel jbrockmendel added the datetime.date stdlib datetime.date support label Jun 5, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@sam-s
Copy link

sam-s commented May 24, 2024

So, what are the plans wrt this?
This is a regression bug that seems to have been tenured into a feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug datetime.date stdlib datetime.date support Datetime Datetime data dtype Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants