Skip to content

BUG: DatetimeIndex.intersection gives incorrect result #42104

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
2 of 3 tasks
grotmol opened this issue Jun 18, 2021 · 3 comments · Fixed by #42115
Closed
2 of 3 tasks

BUG: DatetimeIndex.intersection gives incorrect result #42104

grotmol opened this issue Jun 18, 2021 · 3 comments · Fixed by #42115
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version setops union, intersection, difference, symmetric_difference
Milestone

Comments

@grotmol
Copy link

grotmol commented Jun 18, 2021

  • 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

import pandas as pd
index = pd.DatetimeIndex(['2018-12-31', '2019-03-31', '2019-06-30', '2019-09-30', '2019-12-31', '2020-03-31'], freq='Q-DEC')
print(index[::2].intersection(index[1::2]))

Problem description

The produced output is:
DatetimeIndex(['2019-06-30', '2019-12-31'], dtype='datetime64[ns]', freq='2Q-DEC')

This is clearly incorrect, as there is no overlap between the indices index[::2] and index[1::2].

The issue is related to the freq attribute, which is set to '2Q-DEC' for both index[::2] and index[1::2]. If I remove or change the freq attribute, then it works correctly.

Expected Output

DatetimeIndex([], dtype='datetime64[ns]', freq=None)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 20.5.0
Version : Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0
Cython : 0.29.17
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : 1.4.3
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : None
IPython : 7.21.0
pandas_datareader: 0.9.0
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.23
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : 1.3.0
numba : 0.52.0

@grotmol grotmol added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 18, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jun 18, 2021
@simonjayhawkins
Copy link
Member

Expected Output

DatetimeIndex([], dtype='datetime64[ns]', freq=None)

This was the output in pandas 1.0.5

first bad commit: [29c820f] BUG: DTI/TDI intersection result names (#33904)

cc @jbrockmendel

@simonjayhawkins simonjayhawkins added Frequency DateOffsets 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 Jun 18, 2021
@jbrockmendel jbrockmendel added the setops union, intersection, difference, symmetric_difference label Jun 18, 2021
@jbrockmendel
Copy link
Member

changing _can_fast_intersect to return self.freq.n == 1 on the last line appears to solve this. still need to run the full test suite

jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Jun 18, 2021
@lithomas1 lithomas1 added this to the 1.3 milestone Jun 20, 2021
@grotmol
Copy link
Author

grotmol commented Jun 21, 2021

@simonjayhawkins @jbrockmendel: Thanks for fixing this so swiftly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version setops union, intersection, difference, symmetric_difference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants