Skip to content

Commit f41e929

Browse files
committed
Merge pull request #4497 from stenri/tslib_tz_convert_trans_pos_plus_1__bugfix
Fix issue #4496: tslib.tz_convert(vals, tz1, tz2) may raise an IndexErro...
2 parents bf169ed + 7df8149 commit f41e929

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
197197
- raising an invalid ``TypeError`` rather than ``ValueError`` when appending
198198
with a different block ordering (:issue:`4096`)
199199
- ``read_hdf`` was not respecting as passed ``mode`` (:issue:`4504`)
200+
- Fixed bug in tslib.tz_convert(vals, tz1, tz2): it could raise IndexError exception while
201+
trying to access trans[pos + 1] (:issue:`4496`)
200202
- The ``by`` argument now works correctly with the ``layout`` argument
201203
(:issue:`4102`, :issue:`4014`) in ``*.hist`` plotting methods
202204
- Fixed bug in ``PeriodIndex.map`` where using ``str`` would return the str

pandas/tests/test_frame.py

+19
Original file line numberDiff line numberDiff line change
@@ -10587,6 +10587,25 @@ def test_consolidate_datetime64(self):
1058710587
assert_array_equal(df.starting.values, ser_starting.index.values)
1058810588
assert_array_equal(df.ending.values, ser_ending.index.values)
1058910589

10590+
def test_tslib_tz_convert_trans_pos_plus_1__bug(self):
10591+
"""
10592+
Regression test for tslib.tz_convert(vals, tz1, tz2).
10593+
See https://github.com/pydata/pandas/issues/4496 for details.
10594+
"""
10595+
idx = pd.date_range(datetime(2011, 3, 26, 23), datetime(2011, 3, 27, 1), freq='1min')
10596+
idx = idx.tz_localize('UTC')
10597+
idx = idx.tz_convert('Europe/Moscow')
10598+
10599+
test_vector = pd.Series([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
10600+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
10601+
3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
10602+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
10603+
4, 4, 4, 4, 4, 4, 4, 4, 5], dtype=int)
10604+
10605+
hours = idx.hour
10606+
10607+
np.testing.assert_equal(hours, test_vector.values)
10608+
1059010609
def _check_bool_op(self, name, alternative, frame=None, has_skipna=True,
1059110610
has_bool_only=False):
1059210611
if frame is None:

pandas/tslib.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1432,9 +1432,11 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
14321432
pos -= 1
14331433

14341434
offset = deltas[pos]
1435+
cdef Py_ssize_t trans_len = len(trans)
1436+
14351437
for i in range(n):
14361438
v = utc_dates[i]
1437-
if v >= trans[pos + 1]:
1439+
if (pos + 1) < trans_len and v >= trans[pos + 1]:
14381440
pos += 1
14391441
offset = deltas[pos]
14401442
result[i] = v + offset

0 commit comments

Comments
 (0)