Skip to content

Commit e52ff84

Browse files
committed
Merge pull request #4562 from prossahl/AHLRAP-3065-01
BUG: Fix Segfault with pd.tslib.get_period_field (GH4519, GH4520)
2 parents b5ee28b + 0ee4d93 commit e52ff84

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ pandas 0.13
184184
- Fix arithmetic with series/datetimeindex and ``np.timedelta64`` not working the same (:issue:`4134`)
185185
and buggy timedelta in numpy 1.6 (:issue:`4135`)
186186
- Fix bug in ``pd.read_clipboard`` on windows with PY3 (:issue:`4561`); not decoding properly
187+
- ``tslib.get_period_field()`` and ``tslib.get_period_field_arr()`` now raise
188+
if code argument out of range (:issue:`4519`, :issue:`4520`)
187189

188190
pandas 0.12
189191
===========

pandas/tests/test_tseries.py

+10
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,16 @@ def test_to_datetime_bijective(self):
700700
self.assertEqual(Timestamp(Timestamp.max.to_pydatetime()).value/1000, Timestamp.max.value/1000)
701701
self.assertEqual(Timestamp(Timestamp.min.to_pydatetime()).value/1000, Timestamp.min.value/1000)
702702

703+
class TestPeriodField(unittest.TestCase):
704+
705+
def test_get_period_field_raises_on_out_of_range(self):
706+
from pandas import tslib
707+
self.assertRaises(ValueError, tslib.get_period_field, -1, 0, 0)
708+
709+
def test_get_period_field_array_raises_on_out_of_range(self):
710+
from pandas import tslib
711+
self.assertRaises(ValueError, tslib.get_period_field_arr, -1, np.empty(1), 0)
712+
703713
if __name__ == '__main__':
704714
import nose
705715
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tslib.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,8 @@ ctypedef int (*accessor)(int64_t ordinal, int freq) except INT32_MIN
24192419

24202420
def get_period_field(int code, int64_t value, int freq):
24212421
cdef accessor f = _get_accessor_func(code)
2422+
if f is NULL:
2423+
raise ValueError('Unrecognized period code: %d' % code)
24222424
return f(value, freq)
24232425

24242426
def get_period_field_arr(int code, ndarray[int64_t] arr, int freq):
@@ -2428,6 +2430,8 @@ def get_period_field_arr(int code, ndarray[int64_t] arr, int freq):
24282430
accessor f
24292431

24302432
f = _get_accessor_func(code)
2433+
if f is NULL:
2434+
raise ValueError('Unrecognized period code: %d' % code)
24312435

24322436
sz = len(arr)
24332437
out = np.empty(sz, dtype=np.int64)
@@ -2462,8 +2466,7 @@ cdef accessor _get_accessor_func(int code):
24622466
return &pday_of_year
24632467
elif code == 10:
24642468
return &pweekday
2465-
else:
2466-
raise ValueError('Unrecognized code: %s' % code)
2469+
return NULL
24672470

24682471

24692472
def extract_ordinals(ndarray[object] values, freq):

0 commit comments

Comments
 (0)