Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2415d8a

Browse files
committedFeb 22, 2016
COMPAT: fixes for numpy compat
test_period and slicing exception with nan compat in sparse for floordiv with 0, now returns NaN rather than inf closes #12406 Author: Jeff Reback <[email protected]> Closes #12418 from jreback/numpy_compat and squashes the following commits: fa2c705 [Jeff Reback] COMPAT: fixes for numpy compat
1 parent 8975e7c commit 2415d8a

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed
 

‎pandas/src/sparse.pyx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import numpy as np
77
import operator
88
import sys
99

10+
from distutils.version import LooseVersion
11+
12+
# numpy versioning
13+
_np_version = np.version.short_version
14+
_np_version_under1p8 = LooseVersion(_np_version) < '1.8'
15+
_np_version_under1p9 = LooseVersion(_np_version) < '1.9'
16+
_np_version_under1p10 = LooseVersion(_np_version) < '1.10'
17+
_np_version_under1p11 = LooseVersion(_np_version) < '1.11'
18+
1019
np.import_array()
1120
np.import_ufunc()
1221

@@ -1001,12 +1010,14 @@ cdef inline float64_t __rdiv(float64_t a, float64_t b):
10011010

10021011
cdef inline float64_t __floordiv(float64_t a, float64_t b):
10031012
if b == 0:
1004-
if a > 0:
1005-
return INF
1006-
elif a < 0:
1007-
return -INF
1008-
else:
1009-
return NaN
1013+
# numpy >= 1.11 returns NaN
1014+
# for a // 0, rather than +-inf
1015+
if _np_version_under1p11:
1016+
if a > 0:
1017+
return INF
1018+
elif a < 0:
1019+
return -INF
1020+
return NaN
10101021
else:
10111022
return a // b
10121023

‎pandas/tseries/tests/test_period.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
from pandas.compat import range, lrange, lmap, zip, text_type, PY3
2626
from pandas.compat.numpy_compat import np_datetime64_compat
2727

28-
from pandas import Series, DataFrame, _np_version_under1p9
28+
from pandas import (Series, DataFrame,
29+
_np_version_under1p9, _np_version_under1p11)
2930
from pandas import tslib
3031
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
3132
assertRaisesRegexp)
@@ -2580,12 +2581,15 @@ def test_range_slice_day(self):
25802581
didx = DatetimeIndex(start='2013/01/01', freq='D', periods=400)
25812582
pidx = PeriodIndex(start='2013/01/01', freq='D', periods=400)
25822583

2584+
# changed to TypeError in 1.11
2585+
exc = IndexError if _np_version_under1p11 else TypeError
2586+
25832587
for idx in [didx, pidx]:
25842588
# slices against index should raise IndexError
25852589
values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
25862590
'2013/02/01 09:00']
25872591
for v in values:
2588-
with tm.assertRaises(IndexError):
2592+
with tm.assertRaises(exc):
25892593
idx[v:]
25902594

25912595
s = Series(np.random.rand(len(idx)), index=idx)
@@ -2597,7 +2601,7 @@ def test_range_slice_day(self):
25972601

25982602
invalid = ['2013/02/01 9H', '2013/02/01 09:00']
25992603
for v in invalid:
2600-
with tm.assertRaises(IndexError):
2604+
with tm.assertRaises(exc):
26012605
idx[v:]
26022606

26032607
def test_getitem_seconds(self):
@@ -2634,12 +2638,15 @@ def test_range_slice_seconds(self):
26342638
periods=4000)
26352639
pidx = PeriodIndex(start='2013/01/01 09:00:00', freq='S', periods=4000)
26362640

2641+
# changed to TypeError in 1.11
2642+
exc = IndexError if _np_version_under1p11 else TypeError
2643+
26372644
for idx in [didx, pidx]:
26382645
# slices against index should raise IndexError
26392646
values = ['2014', '2013/02', '2013/01/02', '2013/02/01 9H',
26402647
'2013/02/01 09:00']
26412648
for v in values:
2642-
with tm.assertRaises(IndexError):
2649+
with tm.assertRaises(exc):
26432650
idx[v:]
26442651

26452652
s = Series(np.random.rand(len(idx)), index=idx)

0 commit comments

Comments
 (0)
Please sign in to comment.