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: DatetimeIndex.get_indexer wrong result on mixed-type argument #33741

Closed
2 of 3 tasks
batterseapower opened this issue Apr 23, 2020 · 3 comments · Fixed by #33749
Closed
2 of 3 tasks

BUG: DatetimeIndex.get_indexer wrong result on mixed-type argument #33741

batterseapower opened this issue Apr 23, 2020 · 3 comments · Fixed by #33749
Labels
Bug Index Related to the Index class or subclasses Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@batterseapower
Copy link
Contributor

batterseapower commented Apr 23, 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.

Code Sample, a copy-pastable example

import datetime
import pandas as pd

dates = pd.bdate_range('2020-04-20', '2020-04-25')
print(dates.get_indexer([datetime.date(2020, 4, 20), datetime.date(2020, 4, 21)]))
# [0, 1]
print(dates.get_indexer([pd.Timestamp('2020-04-20'), datetime.date(2020, 4, 21)]))
# [0, -1]

Problem description

As a general rule I would expect that index.get_indexer(as + bs) == np.concat([index.get_indexer(as), index.get_indexer(bs)]) for any lists as and bs, i.e. index.get_indexer operates elementwise. However in this case the type of a previous argument is surprisingly affecting the index that is assigned to a later one.

Expected Output

[0, 1] in both cases

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.8.2.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.936

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@batterseapower batterseapower added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 23, 2020
@dsaxton dsaxton added Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 23, 2020
@dsaxton
Copy link
Member

dsaxton commented Apr 23, 2020

@batterseapower Thanks, the issue is on master as well and appears to be caused by a bug in the type inference for Index itself:

[ins] In [21]: pd.Index([datetime.date(2020, 4, 20), pd.Timestamp("2020-04-21")]).inferred_type                                                                                                              
Out[21]: 'date'

[ins] In [22]: pd.Index([pd.Timestamp("2020-04-21"), datetime.date(2020, 4, 20)]).inferred_type                                                                                                              
Out[22]: 'mixed'

I think fixing that would make get_indexer work as expected (if you try just changing the order of the list in the original example it should give the right output).

@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Apr 23, 2020
@simonjayhawkins
Copy link
Member

This was giving the expected result in 0.25.3 so will mark as regression.

>>> import numpy as np
>>> import pandas as pd
>>>
>>> print(pd.__version__)
0.25.3
>>>
>>> import datetime
>>>
>>> dates = pd.bdate_range("2020-04-20", "2020-04-25")
>>> dates
DatetimeIndex(['2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23',
               '2020-04-24'],
              dtype='datetime64[ns]', freq='B')
>>>
>>> print(dates.get_indexer([datetime.date(2020, 4, 20), datetime.date(2020, 4, 21)]))
[0 1]
>>>
>>> print(dates.get_indexer([pd.Timestamp("2020-04-20"), datetime.date(2020, 4, 21)]))
[0 1]
>>>
>>>

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version and removed Bug labels Apr 24, 2020
@simonjayhawkins
Copy link
Member

This was giving the expected result in 0.25.3 so will mark as regression.

not always. and running code sample on master many times sometimes produces the correct answer.

had a quick go at creating a test and using pytest-repeat, but either always passes or always fails.

unable to bisect, so will remove the regression tag.

@simonjayhawkins simonjayhawkins added Bug and removed Regression Functionality that used to work in a prior pandas version labels Apr 24, 2020
@jreback jreback added this to the 1.1 milestone Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants