Skip to content

Commit 5f2d9b5

Browse files
committed
CLN: Removed pandas.util.testing.choice
Removed pandas.util.testing.choice() method. Replaced all references to it with calls to np.random.choice(). Added entry to whatsnew. #12386
1 parent 1343011 commit 5f2d9b5

File tree

7 files changed

+22
-29
lines changed

7 files changed

+22
-29
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1195,3 +1195,4 @@ Bug Fixes
11951195
- Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`)
11961196
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
11971197
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:`11934`)
1198+
- Removed pandas.util.testing.choice(). Should use np.random.choice(), instead. (:issue:`12386`)

pandas/tests/frame/test_query_eval.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class TestDataFrameQueryWithMultiIndex(tm.TestCase):
9696

9797
def check_query_with_named_multiindex(self, parser, engine):
9898
tm.skip_if_no_ne(engine)
99-
a = tm.choice(['red', 'green'], size=10)
100-
b = tm.choice(['eggs', 'ham'], size=10)
99+
a = np.random.choice(['red', 'green'], size=10)
100+
b = np.random.choice(['eggs', 'ham'], size=10)
101101
index = MultiIndex.from_arrays([a, b], names=['color', 'food'])
102102
df = DataFrame(randn(10, 2), index=index)
103103
ind = Series(df.index.get_level_values('color').values, index=index,
@@ -149,8 +149,8 @@ def test_query_with_named_multiindex(self):
149149

150150
def check_query_with_unnamed_multiindex(self, parser, engine):
151151
tm.skip_if_no_ne(engine)
152-
a = tm.choice(['red', 'green'], size=10)
153-
b = tm.choice(['eggs', 'ham'], size=10)
152+
a = np.random.choice(['red', 'green'], size=10)
153+
b = np.random.choice(['eggs', 'ham'], size=10)
154154
index = MultiIndex.from_arrays([a, b])
155155
df = DataFrame(randn(10, 2), index=index)
156156
ind = Series(df.index.get_level_values(0).values, index=index)
@@ -243,7 +243,7 @@ def test_query_with_unnamed_multiindex(self):
243243

244244
def check_query_with_partially_named_multiindex(self, parser, engine):
245245
tm.skip_if_no_ne(engine)
246-
a = tm.choice(['red', 'green'], size=10)
246+
a = np.random.choice(['red', 'green'], size=10)
247247
b = np.arange(10)
248248
index = MultiIndex.from_arrays([a, b])
249249
index.names = [None, 'rating']
@@ -975,7 +975,7 @@ def check_query_lex_compare_strings(self, parser, engine):
975975
tm.skip_if_no_ne(engine=engine)
976976
import operator as opr
977977

978-
a = Series(tm.choice(list('abcde'), 20))
978+
a = Series(np.random.choice(list('abcde'), 20))
979979
b = Series(np.arange(a.size))
980980
df = DataFrame({'X': a, 'Y': b})
981981

pandas/tests/test_graphics.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def setUp(self):
6060

6161
n = 100
6262
with tm.RNGContext(42):
63-
gender = tm.choice(['Male', 'Female'], size=n)
64-
classroom = tm.choice(['A', 'B', 'C'], size=n)
63+
gender = np.random.choice(['Male', 'Female'], size=n)
64+
classroom = np.random.choice(['A', 'B', 'C'], size=n)
6565

6666
self.hist_df = DataFrame({'gender': gender,
6767
'classroom': classroom,
@@ -3861,7 +3861,7 @@ def test_series_groupby_plotting_nominally_works(self):
38613861
weight = Series(np.random.normal(166, 20, size=n))
38623862
height = Series(np.random.normal(60, 10, size=n))
38633863
with tm.RNGContext(42):
3864-
gender = tm.choice(['male', 'female'], size=n)
3864+
gender = np.random.choice(['male', 'female'], size=n)
38653865

38663866
weight.groupby(gender).plot()
38673867
tm.close()

pandas/tests/test_graphics_others.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def test_grouped_plot_fignums(self):
641641
weight = Series(np.random.normal(166, 20, size=n))
642642
height = Series(np.random.normal(60, 10, size=n))
643643
with tm.RNGContext(42):
644-
gender = tm.choice(['male', 'female'], size=n)
644+
gender = np.random.choice(['male', 'female'], size=n)
645645
df = DataFrame({'height': height, 'weight': weight, 'gender': gender})
646646
gb = df.groupby('gender')
647647

@@ -715,7 +715,7 @@ def test_grouped_hist_legacy2(self):
715715
weight = Series(np.random.normal(166, 20, size=n))
716716
height = Series(np.random.normal(60, 10, size=n))
717717
with tm.RNGContext(42):
718-
gender_int = tm.choice([0, 1], size=n)
718+
gender_int = np.random.choice([0, 1], size=n)
719719
df_int = DataFrame({'height': height, 'weight': weight,
720720
'gender': gender_int})
721721
gb = df_int.groupby('gender')

pandas/tools/tests/test_merge.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,27 @@ def test_join_on(self):
236236

237237
def test_join_on_fails_with_different_right_index(self):
238238
with tm.assertRaises(ValueError):
239-
df = DataFrame({'a': tm.choice(['m', 'f'], size=3),
239+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
240240
'b': np.random.randn(3)})
241-
df2 = DataFrame({'a': tm.choice(['m', 'f'], size=10),
241+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
242242
'b': np.random.randn(10)},
243243
index=tm.makeCustomIndex(10, 2))
244244
merge(df, df2, left_on='a', right_index=True)
245245

246246
def test_join_on_fails_with_different_left_index(self):
247247
with tm.assertRaises(ValueError):
248-
df = DataFrame({'a': tm.choice(['m', 'f'], size=3),
248+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
249249
'b': np.random.randn(3)},
250250
index=tm.makeCustomIndex(10, 2))
251-
df2 = DataFrame({'a': tm.choice(['m', 'f'], size=10),
251+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
252252
'b': np.random.randn(10)})
253253
merge(df, df2, right_on='b', left_index=True)
254254

255255
def test_join_on_fails_with_different_column_counts(self):
256256
with tm.assertRaises(ValueError):
257-
df = DataFrame({'a': tm.choice(['m', 'f'], size=3),
257+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
258258
'b': np.random.randn(3)})
259-
df2 = DataFrame({'a': tm.choice(['m', 'f'], size=10),
259+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
260260
'b': np.random.randn(10)},
261261
index=tm.makeCustomIndex(10, 2))
262262
merge(df, df2, right_on='a', left_on=['a', 'b'])

pandas/util/testing.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def randbool(size=(), p=0.5):
133133

134134
def rands_array(nchars, size, dtype='O'):
135135
"""Generate an array of byte strings."""
136-
retval = (choice(RANDS_CHARS, size=nchars * np.prod(size))
136+
retval = (np.random.choice(RANDS_CHARS, size=nchars * np.prod(size))
137137
.view((np.str_, nchars)).reshape(size))
138138
if dtype is None:
139139
return retval
@@ -143,7 +143,7 @@ def rands_array(nchars, size, dtype='O'):
143143

144144
def randu_array(nchars, size, dtype='O'):
145145
"""Generate an array of unicode strings."""
146-
retval = (choice(RANDU_CHARS, size=nchars * np.prod(size))
146+
retval = (np.random.choice(RANDU_CHARS, size=nchars * np.prod(size))
147147
.view((np.unicode_, nchars)).reshape(size))
148148
if dtype is None:
149149
return retval
@@ -158,7 +158,7 @@ def rands(nchars):
158158
See `rands_array` if you want to create an array of random strings.
159159
160160
"""
161-
return ''.join(choice(RANDS_CHARS, nchars))
161+
return ''.join(np.random.choice(RANDS_CHARS, nchars))
162162

163163

164164
def randu(nchars):
@@ -171,14 +171,6 @@ def randu(nchars):
171171
return ''.join(choice(RANDU_CHARS, nchars))
172172

173173

174-
def choice(x, size=10):
175-
"""sample with replacement; uniform over the input"""
176-
try:
177-
return np.random.choice(x, size=size)
178-
except AttributeError:
179-
return np.random.randint(len(x), size=size).choose(x)
180-
181-
182174
def close(fignum=None):
183175
from matplotlib.pyplot import get_fignums, close as _close
184176

vb_suite/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def f():
143143
value2 = np.random.randn(n)
144144
value2[np.random.rand(n) > 0.5] = np.nan
145145
146-
obj = tm.choice(list('ab'), size=n).astype(object)
146+
obj = np.random.choice(list('ab'), size=n).astype(object)
147147
obj[np.random.randn(n) > 0.5] = np.nan
148148
149149
df = DataFrame({'key1': np.random.randint(0, 500, size=n),

0 commit comments

Comments
 (0)