Skip to content

TST: Parametrize and cleanup Exception #28478

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
merged 7 commits into from
Sep 17, 2019
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
37 changes: 13 additions & 24 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,6 @@ def assert_invalid_comparison(left, right, box):
right >= left


def assert_all(obj):
"""
Test helper to call call obj.all() the appropriate number of times on
a Series or DataFrame.
"""
if isinstance(obj, pd.DataFrame):
assert obj.all().all()
else:
assert obj.all()


# ------------------------------------------------------------------
# Comparisons

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

# The aware==aware and naive==naive comparisons should *not* raise
assert_all(dr == dr)
assert_all(dr == list(dr))
assert_all(list(dr) == dr)
assert_all(np.array(list(dr), dtype=object) == dr)
assert_all(dr == np.array(list(dr), dtype=object))

assert_all(dz == dz)
assert_all(dz == list(dz))
assert_all(list(dz) == dz)
assert_all(np.array(list(dz), dtype=object) == dz)
assert_all(dz == np.array(list(dz), dtype=object))
assert np.all(dr == dr)
assert np.all(dr == list(dr))
assert np.all(list(dr) == dr)
assert np.all(np.array(list(dr), dtype=object) == dr)
assert np.all(dr == np.array(list(dr), dtype=object))

assert np.all(dz == dz)
assert np.all(dz == list(dz))
assert np.all(list(dz) == dz)
assert np.all(np.array(list(dz), dtype=object) == dz)
assert np.all(dz == np.array(list(dz), dtype=object))

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

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

assert_all(dz > ts_tz)
assert np.all(dz > ts_tz)
with pytest.raises(TypeError, match=msg):
op(dz, ts)

Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2160,23 +2160,6 @@ def test_iat(self, float_frame):
expected = float_frame.at[row, col]
assert result == expected

def test_nested_exception(self):
# Ignore the strange way of triggering the problem
# (which may get fixed), it's just a way to trigger
# the issue or reraising an outer exception without
# a named argument
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(
["a", "b"]
)
index = list(df.index)
index[0] = ["a", "b"]
df.index = index

try:
repr(df)
except Exception as e:
assert type(e) != UnboundLocalError

@pytest.mark.parametrize(
"method,expected_values",
[
Expand Down
23 changes: 12 additions & 11 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,23 @@ def _print(result, error=None):
try:
rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))

try:
xp = self.get_result(obj, method2, k2, a)
except Exception:
result = "no comp"
_print(result)
return
with catch_warnings(record=True):
filterwarnings("ignore", "\\n.ix", FutureWarning)
try:
xp = self.get_result(obj, method2, k2, a)
except (KeyError, IndexError):
# TODO: why is this allowed?
result = "no comp"
_print(result)
return

detail = None

try:
if is_scalar(rs) and is_scalar(xp):
assert rs == xp
elif xp.ndim == 1:
tm.assert_series_equal(rs, xp)
elif xp.ndim == 2:
tm.assert_frame_equal(rs, xp)
else:
tm.assert_equal(rs, xp)
result = "ok"
except AssertionError as e:
detail = str(e)
Expand All @@ -242,7 +243,7 @@ def _print(result, error=None):

except AssertionError:
raise
except Exception as detail:
except (IndexError, TypeError, KeyError) as detail:

# if we are in fails, the ok, otherwise raise it
if fails is not None:
Expand Down
16 changes: 4 additions & 12 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def test_dt_accessor_api_for_categorical(self):
("floor", ("D",), {}),
("ceil", ("D",), {}),
("asfreq", ("D",), {}),
# FIXME: don't leave commented-out
# ('tz_localize', ("UTC",), {}),
]
_special_func_names = [f[0] for f in special_func_defs]
Expand Down Expand Up @@ -729,20 +730,11 @@ def test_dt_accessor_api_for_categorical(self):
res = getattr(c.dt, func)(*args, **kwargs)
exp = getattr(s.dt, func)(*args, **kwargs)

if isinstance(res, DataFrame):
tm.assert_frame_equal(res, exp)
elif isinstance(res, Series):
tm.assert_series_equal(res, exp)
else:
tm.assert_almost_equal(res, exp)
tm.assert_equal(res, exp)

for attr in attr_names:
try:
res = getattr(c.dt, attr)
exp = getattr(s.dt, attr)
except Exception as e:
print(name, attr)
raise e
res = getattr(c.dt, attr)
exp = getattr(s.dt, attr)

if isinstance(res, DataFrame):
tm.assert_frame_equal(res, exp)
Expand Down
Loading