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: Reindexing a DatetimeIndex during Daylight Saving Transition Causes Segmentation Fault with dateutil tz #40817

Closed
2 of 3 tasks
jonwiggins opened this issue Apr 6, 2021 · 4 comments
Labels
Bug Segfault Non-Recoverable Error Timezones Timezone data dtype
Milestone

Comments

@jonwiggins
Copy link
Contributor

jonwiggins commented Apr 6, 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 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

@jonwiggins jonwiggins added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 6, 2021
@mroeschke
Copy link
Member

This looks specific to dateutil timezones

In [2]: import datetime
   ...: import pandas as pd
   ...: from dateutil.tz import tzfile
   ...:
   ...: tz = "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"])
Out[2]:
      dummy_values
index
1              NaN
2              NaN

which #33931 might be the root cause. Would be good to keep this issue open as a test case once the issue above is fixed

@mroeschke mroeschke added Timezones Timezone data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 7, 2021
@mroeschke mroeschke changed the title BUG: Reindexing a DatetimeIndex during Daylight Saving Transition Causes Segmentation Fault BUG: Reindexing a DatetimeIndex during Daylight Saving Transition Causes Segmentation Fault with dateutil tz Apr 7, 2021
@jbrockmendel jbrockmendel added the Segfault Non-Recoverable Error label Apr 12, 2021
@alexmemory
Copy link

Could this be a related problem?

$ python3 -q -X faulthandler
>>> import pandas as pd
>>> pd.__version__
'1.2.4'
>>> import dateutil
>>> dateutil.__version__
'2.8.1'
>>> set([pd.Timestamp('2018-11-04 01:00:00-0400'),pd.Timestamp('2018-11-04 01:00:00-0500')])
{Timestamp('2018-11-04 01:00:00-0500', tz='pytz.FixedOffset(-300)'), Timestamp('2018-11-04 01:00:00-0400', tz='pytz.FixedOffset(-240)')}
>>> set([pd.Timestamp('2018-11-04 01:00:00-0400', tz='tzlocal()'),pd.Timestamp('2018-11-04 01:00:00-0400', tz='tzlocal()')])
{Timestamp('2018-11-04 01:00:00-0400', tz='tzlocal()')}
>>> set([pd.Timestamp('2018-11-04 01:00:00-0400', tz='tzlocal()'),pd.Timestamp('2018-11-04 01:00:00-0500', tz='tzlocal()')])
Fatal Python error: Segmentation fault

Current thread 0x00007f1b175c2740 (most recent call first):
  File ".............../lib/python3.7/site-packages/dateutil/tz/tz.py", line 1814 in _datetime_to_timestamp
  File ".............../lib/python3.7/site-packages/dateutil/tz/tz.py", line 259 in _naive_is_dst
  File ".............../lib/python3.7/site-packages/dateutil/tz/tz.py", line 291 in _isdst
  File ".............../lib/python3.7/site-packages/dateutil/tz/tz.py", line 222 in utcoffset
  File "<stdin>", line 1 in <module>
Segmentation fault

@jonwiggins
Copy link
Contributor Author

jonwiggins commented Nov 1, 2021

Just going to update to say that I saw this issue again this weekend when the America/Chihuahua timezone switch DST.

To anyone also having this problem, I believe it can be fixed by using a fill method and then overriding the fill like:

reindexed = df.reindex(dt_values, method="pad")
reindexed[~reindexed.index.isin(df.index)] = 0

@jreback jreback added this to the 1.4 milestone Dec 31, 2021
@rhshadrach
Copy link
Member

On both main and 1.4.x, with the example in the OP I'm seeing the result

      dummy_values
index             
1              NaN
2              NaN

which is correct. The linked PR reported to close this issue, but it did not get automatically closed because it had two issues mentioned on a single line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Segfault Non-Recoverable Error Timezones Timezone data dtype
Projects
None yet
Development

No branches or pull requests

6 participants