8000 Merge pull request #7285 from gfyoung/pandas_randint_compat · numpy/numpy@bd905a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd905a8

Browse files
committed
Merge pull request #7285 from gfyoung/pandas_randint_compat
BUG: Make Randint Backwards Compatible with Pandas
2 parents 2112e7d + 4f91544 commit bd905a8

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

numpy/random/mtrand/mtrand.pyx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ cdef class RandomState:
11591159
"""
11601160
return disc0_array(self.internal_state, rk_long, size, self.lock)
11611161

1162-
def randint(self, low, high=None, size=None, dtype='l'):
1162+
def randint(self, low, high=None, size=None, dtype=int):
11631163
"""
11641164
randint(low, high=None, size=None, dtype='l')
11651165
@@ -1186,7 +1186,7 @@ cdef class RandomState:
11861186
Desired dtype of the result. All dtypes are determined by their
11871187
name, i.e., 'int64', 'int', etc, so byteorder is not available
11881188
and a specific precision may have different C types depending
1189-
on the platform. The default value is 'l' (C long).
1189+
on the platform. The default value is 'np.int'.
11901190
11911191
.. versionadded:: 1.11.0
11921192
@@ -1234,7 +1234,13 @@ cdef class RandomState:
12341234
raise ValueError("low >= high")
12351235

12361236
with self.lock:
1237-
return randfunc(low, high - 1, size, self.state_address)
1237+
ret = randfunc(low, high - 1, size, self.state_address)
1238+
1239+
if size is None:
1240+
if dtype in (np.bool, np.int, np.long):
1241+
return dtype(ret)
1242+
1243+
return ret
12381244

12391245
def bytes(self, npy_intp length):
12401246
"""

numpy/random/tests/test_random.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,25 @@ class TestRandint(TestCase):
137137
rfunc = np.random.randint
138138

139139
# valid integer/boolean types
140-
itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
140+
itype = [np.bool_, np.int8, np.uint8, np.int16, np.uint16,
141141
np.int32, np.uint32, np.int64, np.uint64]
142142

143143
def test_unsupported_type(self):
144144
assert_raises(TypeError, self.rfunc, 1, dtype=np.float)
145145

146146
def test_bounds_checking(self):
147147
for dt in self.itype:
148-
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
149-
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
148+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
149+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
150150
assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt)
151151
assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt)
152152
assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt)
153153
assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt)
154154

155155
def test_rng_zero_and_extremes(self):
156156
for dt in self.itype:
157-
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
158-
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
157+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
158+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
159159
tgt = ubnd - 1
160160
assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
161161
tgt = lbnd
@@ -211,11 +211,20 @@ def test_repeatability(self):
211211
def test_respect_dtype_singleton(self):
212212
# See gh-7203
213213
for dt in self.itype:
214+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
215+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
216+
217+
sample = self.rfunc(lbnd, ubnd, dtype=dt)
218+
self.assertEqual(sample.dtype, np.dtype(dt))
219+
220+
for dt in (np.bool, np.int, np.long):
214221
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
215222
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
216223

224+
# gh-7284: Ensure that we get Python data types
217225
sample = self.rfunc(lbnd, ubnd, dtype=dt)
218-
self.assertEqual(sample.dtype, np.dtype(dt))
226+
self.assertFalse(hasattr(sample, 'dtype'))
227+
self.assertEqual(type(sample), dt)
219228

220229

221230
class TestRandomDist(TestCase):

0 commit comments

Comments
 (0)
0