BUG: Reindexing a DatetimeIndex during Daylight Saving Transition Causes Segmentation Fault with dateutil tz · Issue #40817 · pandas-dev/pandas (original) (raw)


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 datetime import pandas as pd from dateutil.tz import tzfile

tz = tzfile("/usr/share/zoneinfo/America/Santiago") dt_values = [pd.Timestamp('2021-04-03 23:00:00-03:00', tz=tz, freq='H'), pd.Timestamp('2021-04-03 23:00:00-04:00', tz=tz, freq='H'),] dummy_values = ["first", "second"] pd.DataFrame({"index": dt_values, "dummy_values": dummy_values}).set_index("index").reindex(["1", "2"])

Problem description

While the above code should replace the DatetimeIndex of the DataFrame with ["1", "2"], it instead causes a segmentation fault.
This seems to only happen during the Daylight Savings transition during this Chilean timezone, and it seems to only happen if the two values are before and after the timezone switch.

While debugging, it seems that sometime between when self._engine.get_indexer is called in pandas/core/indexes/base.py:get_indexer and when utcoffset is called in dateutil/tz/tz.py (the most granular I get get via stepping in pdb) - the value of the second index enters a broken state. Some values from within utcoffset are shown below.

(Pdb) type(dt)
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
(Pdb) dt.to_pydatetime()
datetime.datetime(2021, 4, 3, 23, 0, tzinfo=tzfile('/usr/share/zoneinfo/America/Santiago'))
(Pdb) dt.tzinfo
tzfile('/usr/share/zoneinfo/America/Santiago')
(Pdb) dt.tz
tzfile('/usr/share/zoneinfo/America/Santiago')

# Notable incorrect results
(Pdb) dt.asm8
numpy.datetime64('1970-01-01T00:00:00.000000000')
(Pdb) dt.value
0
(Pdb) dt.to_datetime64()
numpy.datetime64('1970-01-01T00:00:00.000000000')

# Results in a segmentation fault
(Pdb) str(dt)
(Pdb) dt

In pandas version 1.1.4 I was able to solve this problem by setting method="pad" so that a different indexer would be used. However, in pandas 1.2.3 I get segmentation faults for all 3 values (bfill, ffill, None).

Expected Output

I would expect this to run normally - resetting the index of the DataFrame with the index given

import datetime import pandas as pd from dateutil.tz import tzfile

tz = tzfile("/usr/share/zoneinfo/America/Santiago") dt_values = [pd.Timestamp('2021-04-03 23:00:00-03:00', tz=tz, freq='H'), pd.Timestamp('2021-04-03 23:00:00-04:00', tz=tz, freq='H'),] dummy_values = ["first", "second"] pd.DataFrame({"index": dt_values, "dummy_values": dummy_values}).set_index("index").reindex(["1", "2"])

      dummy_values
index
1            first
2           second

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.7.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.3
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 9.0.1
setuptools : 39.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 0.999999999
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None