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 5 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
7 changes: 3 additions & 4 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2172,10 +2172,9 @@ def test_nested_exception(self):
index[0] = ["a", "b"]
df.index = index

try:
repr(df)
except Exception as e:
assert type(e) != UnboundLocalError
# Note: this used to be a test that any exception that _is_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just delete altogether? Maybe missing the point but I don't see what this was testing previously

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was added in #2201 / #2200, so there was a bug in the index assignment that raised a "wrong" error when asking for the repr. But nowadays we just don't create an invalid MultiIndex. So I think we should either just remove this test, or update it to check df.index with the expected value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove

# raised by repr is _not_ an UnboundLocalError.
repr(df)

@pytest.mark.parametrize(
"method,expected_values",
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def _print(result, error=None):

try:
xp = self.get_result(obj, method2, k2, a)
except Exception:
except (KeyError, IndexError):
# TODO: why is this allowed?
result = "no comp"
_print(result)
return
Expand All @@ -222,10 +223,8 @@ def _print(result, error=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 +241,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