Skip to content

BUG: DatetimeIndex.intersection not working for localized indices #46702

Closed
@rwijtvliet

Description

@rwijtvliet

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# this works:
i1 = pd.date_range('2020-03-27', periods=5, freq='D')
i2 = pd.date_range('2020-03-30', periods=5, freq='D')
i1.intersection(i2)  # DatetimeIndex(['2020-03-30', '2020-03-31'], dtype='datetime64[ns]', freq='D')

# but this does not:
i1 = pd.date_range('2020-03-27', periods=5, freq='D', tz='Europe/Berlin')
i2 = pd.date_range('2020-03-30', periods=5, freq='D', tz='Europe/Berlin')
i1.intersection(i2) # DatetimeIndex([], dtype='datetime64[ns, Europe/Berlin]', freq='D')
# should be DatetimeIndex(['2020-03-30 00:00:00+02:00', '2020-03-31 00:00:00+02:00'], dtype='datetime64[ns, Europe/Berlin]', freq='D')

Issue Description

intersection is not working correctly for timezone-aware DatetimeIndex instances.

Expected Behavior

That all timestamps that are in both indices are kept.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.9.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 1.4.1
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : 7.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.1.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : 1.4.32
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeIndexRelated to the Index class or subclassesRegressionFunctionality that used to work in a prior pandas versionTimezonesTimezone data dtypesetopsunion, intersection, difference, symmetric_difference

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions