Bug: tslib.tz_convert(vals, tz1, tz2) may raise an IndexError exception. · Issue #4496 · pandas-dev/pandas (original) (raw)

The following code produces "Out of bounds on buffer access (axis 0)" exception when accessing idx.hour:


import pandas as pd

idx = pd.date_range('26/3/2011', '27/3/2011', freq='1min')
idx = idx.tz_localize('UTC')
idx = idx.tz_convert('Europe/Moscow')

hour = idx.hour


Traceback (most recent call last):
File "test.py", line 7, in
hour = idx.hour
File "C:\Python27\lib\site-packages\pandas\tseries\index.py", line 41, in f
values = self._local_timestamps()
File "C:\Python27\lib\site-packages\pandas\tseries\index.py", line 397, in _local_timestamps
return tslib.tz_convert(self.asi8, utc, self.tz)
File "tslib.pyx", line 1356, in pandas.tslib.tz_convert (pandas\tslib.c:21388)
IndexError: Out of bounds on buffer access (axis 0)

My research indicated there was a bug in the following piece of code in tslib.tz_convert(vals, tz1, tz2) in Pandas 0.12. There is no check that "(pos + 1) < len(trans)" when referencing trans[pos + 1] inside the for loop:

        offset = deltas[pos]
        for i in range(n):
            v = utc_dates[i]
            if v >= trans[pos + 1]:
                pos += 1
                offset = deltas[pos]
            result[i] = v + offset

As a result in some circumstances this code raises "Out of bounds on buffer access (axis 0)" exception. For example, in Russia last day-light saving transition took place in somewhere in 26/3/2011-27/3/2011, and there were no transitions since then.

If ts_lib.tz_convert() is asked to convert time zones for interval that includes 26/3/2011-27/3/2011, it raises an exception while trying to access trans[pos + 1].

I have a working patch that solves this. Going to contribute it soon.