Skip to content

MAINT: Remove self.assertRaisesRegexp from testing #16113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,16 +1874,16 @@ def test_groupby_args(self):
def j():
frame.groupby()

self.assertRaisesRegexp(TypeError,
"You have to supply one of 'by' and 'level'",
j)
tm.assertRaisesRegexp(TypeError,
"You have to supply one of 'by' and 'level'",
j)

def k():
frame.groupby(by=None, level=None)

self.assertRaisesRegexp(TypeError,
"You have to supply one of 'by' and 'level'",
k)
tm.assertRaisesRegexp(TypeError,
"You have to supply one of 'by' and 'level'",
k)

def test_groupby_level_mapper(self):
frame = self.mframe
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def test_transform_with_non_scalar_group(self):
df = pd.DataFrame(np.random.randint(1, 10, (4, 12)),
columns=cols,
index=['A', 'C', 'G', 'T'])
self.assertRaisesRegexp(ValueError, 'transform must return a scalar '
'value for each group.*', df.groupby
(axis=1, level=1).transform,
lambda z: z.div(z.sum(axis=1), axis=0))
tm.assertRaisesRegexp(ValueError, 'transform must return a scalar '
'value for each group.*', df.groupby
(axis=1, level=1).transform,
lambda z: z.div(z.sum(axis=1), axis=0))
6 changes: 3 additions & 3 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ def test_fillna(self):
elif isinstance(index, MultiIndex):
idx = index.copy()
msg = "isnull is not defined for MultiIndex"
with self.assertRaisesRegexp(NotImplementedError, msg):
with tm.assertRaisesRegexp(NotImplementedError, msg):
idx.fillna(idx[0])
else:
idx = index.copy()
Expand All @@ -884,7 +884,7 @@ def test_fillna(self):
self.assertFalse(result is idx)

msg = "'value' must be a scalar, passed: "
with self.assertRaisesRegexp(TypeError, msg):
with tm.assertRaisesRegexp(TypeError, msg):
idx.fillna([idx[0]])

idx = index.copy()
Expand Down Expand Up @@ -918,7 +918,7 @@ def test_nulls(self):
elif isinstance(index, MultiIndex):
idx = index.copy()
msg = "isnull is not defined for MultiIndex"
with self.assertRaisesRegexp(NotImplementedError, msg):
with tm.assertRaisesRegexp(NotImplementedError, msg):
idx.isnull()
else:

Expand Down
32 changes: 16 additions & 16 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,31 +212,31 @@ def test_naive_aware_conflicts(self):
naive = bdate_range(START, END, freq=BDay(), tz=None)
aware = bdate_range(START, END, freq=BDay(),
tz="Asia/Hong_Kong")
self.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
naive.join, aware)
self.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
aware.join, naive)
tm.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
naive.join, aware)
tm.assertRaisesRegexp(TypeError, "tz-naive.*tz-aware",
aware.join, naive)

def test_cached_range(self):
DatetimeIndex._cached_range(START, END, offset=BDay())
DatetimeIndex._cached_range(START, periods=20, offset=BDay())
DatetimeIndex._cached_range(end=START, periods=20, offset=BDay())

self.assertRaisesRegexp(TypeError, "offset",
DatetimeIndex._cached_range,
START, END)
tm.assertRaisesRegexp(TypeError, "offset",
DatetimeIndex._cached_range,
START, END)

self.assertRaisesRegexp(TypeError, "specify period",
DatetimeIndex._cached_range, START,
offset=BDay())
tm.assertRaisesRegexp(TypeError, "specify period",
DatetimeIndex._cached_range, START,
offset=BDay())

self.assertRaisesRegexp(TypeError, "specify period",
DatetimeIndex._cached_range, end=END,
offset=BDay())
tm.assertRaisesRegexp(TypeError, "specify period",
DatetimeIndex._cached_range, end=END,
offset=BDay())

self.assertRaisesRegexp(TypeError, "start or end",
DatetimeIndex._cached_range, periods=20,
offset=BDay())
tm.assertRaisesRegexp(TypeError, "start or end",
DatetimeIndex._cached_range, periods=20,
offset=BDay())

def test_cached_range_bug(self):
rng = date_range('2010-09-01 05:00:00', periods=50,
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,12 @@ def assert_slices_equivalent(l_slc, i_slc):
def test_slice_with_zero_step_raises(self):
ts = Series(np.arange(20),
date_range('2014-01-01', periods=20, freq='MS'))
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])

def test_slice_bounds_empty(self):
# GH 14354
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def test_partial_slice_second_precision(self):
tm.assert_series_equal(s['2005-1-1 00:01:00'], s.iloc[10:])

self.assertEqual(s[Timestamp('2005-1-1 00:00:59.999990')], s.iloc[0])
self.assertRaisesRegexp(KeyError, '2005-1-1 00:00:00',
lambda: s['2005-1-1 00:00:00'])
tm.assertRaisesRegexp(KeyError, '2005-1-1 00:00:00',
lambda: s['2005-1-1 00:00:00'])

def test_partial_slicing_dataframe(self):
# GH14856
Expand Down Expand Up @@ -249,14 +249,14 @@ def test_partial_slice_doesnt_require_monotonicity(self):
timestamp = pd.Timestamp('2014-01-10')

tm.assert_series_equal(nonmonotonic['2014-01-10':], expected)
self.assertRaisesRegexp(KeyError,
r"Timestamp\('2014-01-10 00:00:00'\)",
lambda: nonmonotonic[timestamp:])
tm.assertRaisesRegexp(KeyError,
r"Timestamp\('2014-01-10 00:00:00'\)",
lambda: nonmonotonic[timestamp:])

tm.assert_series_equal(nonmonotonic.loc['2014-01-10':], expected)
self.assertRaisesRegexp(KeyError,
r"Timestamp\('2014-01-10 00:00:00'\)",
lambda: nonmonotonic.loc[timestamp:])
tm.assertRaisesRegexp(KeyError,
r"Timestamp\('2014-01-10 00:00:00'\)",
lambda: nonmonotonic.loc[timestamp:])

def test_loc_datetime_length_one(self):
# GH16071
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def test_getitem_partial(self):
tm.assert_series_equal(exp, result)

ts = ts[10:].append(ts[10:])
self.assertRaisesRegexp(KeyError,
"left slice bound for non-unique "
"label: '2008'",
ts.__getitem__, slice('2008', '2009'))
tm.assertRaisesRegexp(KeyError,
"left slice bound for non-unique "
"label: '2008'",
ts.__getitem__, slice('2008', '2009'))

def test_getitem_datetime(self):
rng = period_range(start='2012-01-01', periods=10, freq='W-MON')
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def assert_slices_equivalent(l_slc, i_slc):
def test_slice_with_zero_step_raises(self):
ts = Series(np.arange(20),
period_range('2014-01', periods=20, freq='M'))
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])

def test_slice_keep_name(self):
idx = period_range('20010101', periods=10, freq='D', name='bob')
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_get_indexer(self):
np.array([0, -1, 1], dtype=np.intp))

msg = 'Input has different freq from PeriodIndex\\(freq=H\\)'
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
idx.get_indexer(target, 'nearest', tolerance='1 minute')

tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest',
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/period/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def test_to_period_monthish(self):
self.assertEqual(prng.freq, 'M')

msg = pd.tseries.frequencies._INVALID_FREQ_ERROR
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
date_range('01-Jan-2012', periods=8, freq='EOM')

def test_period_dt64_round_trip(self):
Expand Down Expand Up @@ -439,11 +439,11 @@ def test_searchsorted(self):
self.assertEqual(pidx.searchsorted(p2), 3)

msg = "Input has different freq=H from PeriodIndex"
with self.assertRaisesRegexp(period.IncompatibleFrequency, msg):
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
pidx.searchsorted(pd.Period('2014-01-01', freq='H'))

msg = "Input has different freq=5D from PeriodIndex"
with self.assertRaisesRegexp(period.IncompatibleFrequency, msg):
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
pidx.searchsorted(pd.Period('2014-01-01', freq='5D'))

with tm.assert_produces_warning(FutureWarning):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,8 +1450,8 @@ def test_str_attribute(self):
MultiIndex.from_tuples([('foo', '1'), ('bar', '3')]),
PeriodIndex(start='2000', end='2010', freq='A')]
for idx in indices:
with self.assertRaisesRegexp(AttributeError,
'only use .str accessor'):
with tm.assertRaisesRegexp(AttributeError,
'only use .str accessor'):
idx.str.repeat(2)

idx = Index(['a b c', 'd e', 'f'])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,9 @@ def test_comparison(self):
actual = self.index == self.index.left
tm.assert_numpy_array_equal(actual, np.array([False, False]))

with self.assertRaisesRegexp(TypeError, 'unorderable types'):
with tm.assertRaisesRegexp(TypeError, 'unorderable types'):
self.index > 0
with self.assertRaisesRegexp(TypeError, 'unorderable types'):
with tm.assertRaisesRegexp(TypeError, 'unorderable types'):
self.index <= 0
with pytest.raises(TypeError):
self.index > np.arange(2)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_round(self):
self.assertEqual(elt.round(freq='H'), expected_elt)

msg = pd.tseries.frequencies._INVALID_FREQ_ERROR
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
td.round(freq='foo')
with tm.assertRaisesRegexp(ValueError, msg):
elt.round(freq='foo')
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/timedeltas/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def assert_slices_equivalent(l_slc, i_slc):

def test_slice_with_zero_step_raises(self):
ts = Series(np.arange(20), timedelta_range('0', periods=20, freq='H'))
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: ts.loc[::0])
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def test_scalar_error(self):

def f():
s.iloc[3.0]
self.assertRaisesRegexp(TypeError,
'cannot do positional indexing',
f)
tm.assertRaisesRegexp(TypeError,
'cannot do positional indexing',
f)

def f():
s.iloc[3.0] = 0
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ def assert_slices_equivalent(l_slc, i_slc):

def test_slice_with_zero_step_raises(self):
s = Series(np.arange(20), index=_mklbl('A', 20))
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s[::0])
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s.loc[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s.loc[::0])
with catch_warnings(record=True):
self.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s.ix[::0])
tm.assertRaisesRegexp(ValueError, 'slice step cannot be zero',
lambda: s.ix[::0])

def test_indexing_assignment_dict_already_exists(self):
df = pd.DataFrame({'x': [1, 2, 6],
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def test_at_to_fail(self):
df.columns = ['x', 'x', 'z']

# Check that we get the correct value in the KeyError
self.assertRaisesRegexp(KeyError, r"\['y'\] not in index",
lambda: df[['x', 'y', 'z']])
tm.assertRaisesRegexp(KeyError, r"\['y'\] not in index",
lambda: df[['x', 'y', 'z']])

def test_at_with_tz(self):
# gh-15822
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ def test_to_jsonl(self):

def test_latin_encoding(self):
if compat.PY2:
self.assertRaisesRegexp(
tm.assertRaisesRegexp(
TypeError, r'\[unicode\] is not implemented as a table column')
return

Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/io/parser/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ def test_zip(self):
tmp.writestr(file_name, data)
tmp.close()

self.assertRaisesRegexp(ValueError, 'Multiple files',
self.read_csv, path, compression='zip')
tm.assertRaisesRegexp(ValueError, 'Multiple files',
self.read_csv, path, compression='zip')

self.assertRaisesRegexp(ValueError, 'Multiple files',
self.read_csv, path, compression='infer')
tm.assertRaisesRegexp(ValueError, 'Multiple files',
self.read_csv, path, compression='infer')

with tm.ensure_clean() as path:
tmp = zipfile.ZipFile(path, mode='w')
tmp.close()

self.assertRaisesRegexp(ValueError, 'Zero files',
self.read_csv, path, compression='zip')
tm.assertRaisesRegexp(ValueError, 'Zero files',
self.read_csv, path, compression='zip')

with tm.ensure_clean() as path:
with open(path, 'wb') as f:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ def test_encoding(self):
def test_latin_encoding(self):

if compat.PY2:
self.assertRaisesRegexp(
tm.assertRaisesRegexp(
TypeError, r'\[unicode\] is not implemented as a table column')
return

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_equal(self):
self.assertNotEqual(Interval(0, 1), 0)

def test_comparison(self):
with self.assertRaisesRegexp(TypeError, 'unorderable types'):
with tm.assertRaisesRegexp(TypeError, 'unorderable types'):
Interval(0, 1) < 2

self.assertTrue(Interval(0, 1) < Interval(1, 2))
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/scalar/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ def test_period_deprecated_freq(self):
msg = pd.tseries.frequencies._INVALID_FREQ_ERROR
for exp, freqs in iteritems(cases):
for freq in freqs:
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
Period('2016-03-01 09:00', freq=freq)
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
Period(ordinal=1, freq=freq)

# check supported freq-aliases still works
Expand Down Expand Up @@ -762,7 +762,7 @@ def test_properties_weekly_legacy(self):
self.assertEqual(exp.days_in_month, 29)

msg = pd.tseries.frequencies._INVALID_FREQ_ERROR
with self.assertRaisesRegexp(ValueError, msg):
with tm.assertRaisesRegexp(ValueError, msg):
Period(freq='WK', year=2007, month=1, day=7)

def test_properties_daily(self):
Expand Down
Loading