Skip to content

Commit 6110608

Browse files
jbrockmendelTomAugspurger
authored andcommitted
TST: Parametrize and cleanup Exception (#28478)
1 parent 6e7fdec commit 6110608

File tree

6 files changed

+145
-181
lines changed

6 files changed

+145
-181
lines changed

pandas/tests/arithmetic/test_datetime64.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,6 @@ def assert_invalid_comparison(left, right, box):
7878
right >= left
7979

8080

81-
def assert_all(obj):
82-
"""
83-
Test helper to call call obj.all() the appropriate number of times on
84-
a Series or DataFrame.
85-
"""
86-
if isinstance(obj, pd.DataFrame):
87-
assert obj.all().all()
88-
else:
89-
assert obj.all()
90-
91-
9281
# ------------------------------------------------------------------
9382
# Comparisons
9483

@@ -578,17 +567,17 @@ def test_comparison_tzawareness_compat(self, op, box_df_fail):
578567
op(dz, np.array(list(dr), dtype=object))
579568

580569
# The aware==aware and naive==naive comparisons should *not* raise
581-
assert_all(dr == dr)
582-
assert_all(dr == list(dr))
583-
assert_all(list(dr) == dr)
584-
assert_all(np.array(list(dr), dtype=object) == dr)
585-
assert_all(dr == np.array(list(dr), dtype=object))
586-
587-
assert_all(dz == dz)
588-
assert_all(dz == list(dz))
589-
assert_all(list(dz) == dz)
590-
assert_all(np.array(list(dz), dtype=object) == dz)
591-
assert_all(dz == np.array(list(dz), dtype=object))
570+
assert np.all(dr == dr)
571+
assert np.all(dr == list(dr))
572+
assert np.all(list(dr) == dr)
573+
assert np.all(np.array(list(dr), dtype=object) == dr)
574+
assert np.all(dr == np.array(list(dr), dtype=object))
575+
576+
assert np.all(dz == dz)
577+
assert np.all(dz == list(dz))
578+
assert np.all(list(dz) == dz)
579+
assert np.all(np.array(list(dz), dtype=object) == dz)
580+
assert np.all(dz == np.array(list(dz), dtype=object))
592581

593582
@pytest.mark.parametrize(
594583
"op",
@@ -606,12 +595,12 @@ def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
606595
ts = pd.Timestamp("2000-03-14 01:59")
607596
ts_tz = pd.Timestamp("2000-03-14 01:59", tz="Europe/Amsterdam")
608597

609-
assert_all(dr > ts)
598+
assert np.all(dr > ts)
610599
msg = "Cannot compare tz-naive and tz-aware"
611600
with pytest.raises(TypeError, match=msg):
612601
op(dr, ts_tz)
613602

614-
assert_all(dz > ts_tz)
603+
assert np.all(dz > ts_tz)
615604
with pytest.raises(TypeError, match=msg):
616605
op(dz, ts)
617606

pandas/tests/frame/test_indexing.py

-17
Original file line numberDiff line numberDiff line change
@@ -2160,23 +2160,6 @@ def test_iat(self, float_frame):
21602160
expected = float_frame.at[row, col]
21612161
assert result == expected
21622162

2163-
def test_nested_exception(self):
2164-
# Ignore the strange way of triggering the problem
2165-
# (which may get fixed), it's just a way to trigger
2166-
# the issue or reraising an outer exception without
2167-
# a named argument
2168-
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(
2169-
["a", "b"]
2170-
)
2171-
index = list(df.index)
2172-
index[0] = ["a", "b"]
2173-
df.index = index
2174-
2175-
try:
2176-
repr(df)
2177-
except Exception as e:
2178-
assert type(e) != UnboundLocalError
2179-
21802163
@pytest.mark.parametrize(
21812164
"method,expected_values",
21822165
[

pandas/tests/indexing/common.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,23 @@ def _print(result, error=None):
210210
try:
211211
rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))
212212

213-
try:
214-
xp = self.get_result(obj, method2, k2, a)
215-
except Exception:
216-
result = "no comp"
217-
_print(result)
218-
return
213+
with catch_warnings(record=True):
214+
filterwarnings("ignore", "\\n.ix", FutureWarning)
215+
try:
216+
xp = self.get_result(obj, method2, k2, a)
217+
except (KeyError, IndexError):
218+
# TODO: why is this allowed?
219+
result = "no comp"
220+
_print(result)
221+
return
219222

220223
detail = None
221224

222225
try:
223226
if is_scalar(rs) and is_scalar(xp):
224227
assert rs == xp
225-
elif xp.ndim == 1:
226-
tm.assert_series_equal(rs, xp)
227-
elif xp.ndim == 2:
228-
tm.assert_frame_equal(rs, xp)
228+
else:
229+
tm.assert_equal(rs, xp)
229230
result = "ok"
230231
except AssertionError as e:
231232
detail = str(e)
@@ -242,7 +243,7 @@ def _print(result, error=None):
242243

243244
except AssertionError:
244245
raise
245-
except Exception as detail:
246+
except (IndexError, TypeError, KeyError) as detail:
246247

247248
# if we are in fails, the ok, otherwise raise it
248249
if fails is not None:

pandas/tests/series/test_api.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ def test_dt_accessor_api_for_categorical(self):
697697
("floor", ("D",), {}),
698698
("ceil", ("D",), {}),
699699
("asfreq", ("D",), {}),
700+
# FIXME: don't leave commented-out
700701
# ('tz_localize', ("UTC",), {}),
701702
]
702703
_special_func_names = [f[0] for f in special_func_defs]
@@ -729,20 +730,11 @@ def test_dt_accessor_api_for_categorical(self):
729730
res = getattr(c.dt, func)(*args, **kwargs)
730731
exp = getattr(s.dt, func)(*args, **kwargs)
731732

732-
if isinstance(res, DataFrame):
733-
tm.assert_frame_equal(res, exp)
734-
elif isinstance(res, Series):
735-
tm.assert_series_equal(res, exp)
736-
else:
737-
tm.assert_almost_equal(res, exp)
733+
tm.assert_equal(res, exp)
738734

739735
for attr in attr_names:
740-
try:
741-
res = getattr(c.dt, attr)
742-
exp = getattr(s.dt, attr)
743-
except Exception as e:
744-
print(name, attr)
745-
raise e
736+
res = getattr(c.dt, attr)
737+
exp = getattr(s.dt, attr)
746738

747739
if isinstance(res, DataFrame):
748740
tm.assert_frame_equal(res, exp)

0 commit comments

Comments
 (0)