-
-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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.intersection
not working for localized indices
#46702
Comments
Current workaround: 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')
pd.Index([i for i in i1 if i in i2], freq=i1.freq) |
can u try on master and see if these r working |
these are only when the transition is happening right? or for all tz aware? (which i know we test so this is odd) |
It's only an issue when there's a DST transition |
Just to make sure I'm doing it right: I created and activated a fresh virtual environment, The result is the same: >>> 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') Whereas time periods not containing DST-start do work: >>> i1 = pd.date_range('2020-03-20', periods=5, freq='D', tz='Europe/Berlin')
>>> i2 = pd.date_range('2020-03-23', periods=5, freq='D', tz='Europe/Berlin')
>>> i1.intersection(i2)
DatetimeIndex(['2020-03-23 00:00:00+01:00', '2020-03-24 00:00:00+01:00'], dtype='datetime64[ns, Europe/Berlin]', freq='D') >>> pd.show_versions()
INSTALLED VERSIONS
------------------
commit : 83f0d71a2cbde738c459e4feca54288b77d00e3d
python : 3.9.12.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.5.0.dev0+666.g83f0d71a2c
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
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
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None |
Thanks for the report. The set-ops in general have issues dealing with DST transitions ( |
My current workaround for anyone interested def intersection(*indices: pd.DatetimeIndex) -> pd.DatetimeIndex:
"""Intersect several DatetimeIndices.
Parameters
----------
*indices : pd.DatetimeIndex
The indices to intersect.
Returns
-------
pd.DatetimeIndex
The intersection, i.e., datetimeindex with values that exist in each index.
Notes
-----
If indices have distinct timezones or names, the values from the first index are used.
"""
if len(indices) == 0:
raise ValueError("Must specify at least one index.")
distinct_freqs = set([i.freq for i in indices])
if len(indices) > 1 and len(distinct_freqs) != 1:
raise ValueError(
f"Indices must not have equal frequencies; got {distinct_freqs}."
)
tznaive = [i.tz is None for i in indices]
if any(tznaive) and not all(tznaive):
raise ValueError(
f"All indices must be either timezone-aware or timezone-naive; got {tznaive} naive from {len(indices)}."
)
freq, name, tz = indices[0].freq, indices[0].name, indices[0].tz
# Calculation is cumbersome: pandas DatetimeIndex.intersection not working correctly on timezone-aware indices.
values = set(indices[0])
for idx in indices[1:]:
values = values.intersection(set(idx))
idx = pd.DatetimeIndex(sorted(list(values)), freq=freq, name=name, tz=tz)
return idx |
moving to 1.4.4 |
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
Issue Description
intersection
is not working correctly for timezone-awareDatetimeIndex
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
The text was updated successfully, but these errors were encountered: