|
7 | 7 | import pytest
|
8 | 8 |
|
9 | 9 | from pandas._libs.sparse import BlockIndex, IntIndex
|
10 |
| -from pandas.compat import lrange |
| 10 | +from pandas.compat import PY2, lrange |
11 | 11 | from pandas.errors import PerformanceWarning
|
12 | 12 |
|
13 | 13 | import pandas as pd
|
@@ -145,8 +145,9 @@ def test_constructor_ndarray(self, float_frame):
|
145 | 145 | tm.assert_sp_frame_equal(sp, float_frame.reindex(columns=['A']))
|
146 | 146 |
|
147 | 147 | # raise on level argument
|
148 |
| - pytest.raises(TypeError, float_frame.reindex, columns=['A'], |
149 |
| - level=1) |
| 148 | + msg = "Reindex by level not supported for sparse" |
| 149 | + with pytest.raises(TypeError, match=msg): |
| 150 | + float_frame.reindex(columns=['A'], level=1) |
150 | 151 |
|
151 | 152 | # wrong length index / columns
|
152 | 153 | with pytest.raises(ValueError, match="^Index length"):
|
@@ -433,7 +434,8 @@ def test_getitem(self):
|
433 | 434 | exp = sdf.reindex(columns=['a', 'b'])
|
434 | 435 | tm.assert_sp_frame_equal(result, exp)
|
435 | 436 |
|
436 |
| - pytest.raises(Exception, sdf.__getitem__, ['a', 'd']) |
| 437 | + with pytest.raises(KeyError, match=r"\['d'\] not in index"): |
| 438 | + sdf[['a', 'd']] |
437 | 439 |
|
438 | 440 | def test_iloc(self, float_frame):
|
439 | 441 |
|
@@ -504,7 +506,9 @@ def test_getitem_overload(self, float_frame):
|
504 | 506 | subframe = float_frame[indexer]
|
505 | 507 |
|
506 | 508 | tm.assert_index_equal(subindex, subframe.index)
|
507 |
| - pytest.raises(Exception, float_frame.__getitem__, indexer[:-1]) |
| 509 | + msg = "Item wrong length 9 instead of 10" |
| 510 | + with pytest.raises(ValueError, match=msg): |
| 511 | + float_frame[indexer[:-1]] |
508 | 512 |
|
509 | 513 | def test_setitem(self, float_frame, float_frame_int_kind,
|
510 | 514 | float_frame_dense,
|
@@ -551,8 +555,9 @@ def _check_frame(frame, orig):
|
551 | 555 | assert len(frame['I'].sp_values) == N // 2
|
552 | 556 |
|
553 | 557 | # insert ndarray wrong size
|
554 |
| - pytest.raises(Exception, frame.__setitem__, 'foo', |
555 |
| - np.random.randn(N - 1)) |
| 558 | + msg = "Length of values does not match length of index" |
| 559 | + with pytest.raises(AssertionError, match=msg): |
| 560 | + frame['foo'] = np.random.randn(N - 1) |
556 | 561 |
|
557 | 562 | # scalar value
|
558 | 563 | frame['J'] = 5
|
@@ -625,17 +630,22 @@ def test_delitem(self, float_frame):
|
625 | 630 |
|
626 | 631 | def test_set_columns(self, float_frame):
|
627 | 632 | float_frame.columns = float_frame.columns
|
628 |
| - pytest.raises(Exception, setattr, float_frame, 'columns', |
629 |
| - float_frame.columns[:-1]) |
| 633 | + msg = ("Length mismatch: Expected axis has 4 elements, new values have" |
| 634 | + " 3 elements") |
| 635 | + with pytest.raises(ValueError, match=msg): |
| 636 | + float_frame.columns = float_frame.columns[:-1] |
630 | 637 |
|
631 | 638 | def test_set_index(self, float_frame):
|
632 | 639 | float_frame.index = float_frame.index
|
633 |
| - pytest.raises(Exception, setattr, float_frame, 'index', |
634 |
| - float_frame.index[:-1]) |
| 640 | + msg = ("Length mismatch: Expected axis has 10 elements, new values" |
| 641 | + " have 9 elements") |
| 642 | + with pytest.raises(ValueError, match=msg): |
| 643 | + float_frame.index = float_frame.index[:-1] |
635 | 644 |
|
636 | 645 | def test_ctor_reindex(self):
|
637 | 646 | idx = pd.Index([0, 1, 2, 3])
|
638 |
| - with pytest.raises(ValueError, match=''): |
| 647 | + msg = "Length of passed values is 2, index implies 4" |
| 648 | + with pytest.raises(ValueError, match=msg): |
639 | 649 | pd.SparseDataFrame({"A": [1, 2]}, index=idx)
|
640 | 650 |
|
641 | 651 | def test_append(self, float_frame):
|
@@ -858,14 +868,18 @@ def test_describe(self, float_frame):
|
858 | 868 | str(float_frame)
|
859 | 869 | desc = float_frame.describe() # noqa
|
860 | 870 |
|
| 871 | + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") |
861 | 872 | def test_join(self, float_frame):
|
862 | 873 | left = float_frame.loc[:, ['A', 'B']]
|
863 | 874 | right = float_frame.loc[:, ['C', 'D']]
|
864 | 875 | joined = left.join(right)
|
865 | 876 | tm.assert_sp_frame_equal(joined, float_frame, exact_indices=False)
|
866 | 877 |
|
867 | 878 | right = float_frame.loc[:, ['B', 'D']]
|
868 |
| - pytest.raises(Exception, left.join, right) |
| 879 | + msg = (r"columns overlap but no suffix specified: Index\(\['B'\]," |
| 880 | + r" dtype='object'\)") |
| 881 | + with pytest.raises(ValueError, match=msg): |
| 882 | + left.join(right) |
869 | 883 |
|
870 | 884 | with pytest.raises(ValueError, match='Other Series must have a name'):
|
871 | 885 | float_frame.join(Series(
|
@@ -1046,8 +1060,11 @@ def _check(frame):
|
1046 | 1060 | _check(float_frame_int_kind)
|
1047 | 1061 |
|
1048 | 1062 | # for now
|
1049 |
| - pytest.raises(Exception, _check, float_frame_fill0) |
1050 |
| - pytest.raises(Exception, _check, float_frame_fill2) |
| 1063 | + msg = "This routine assumes NaN fill value" |
| 1064 | + with pytest.raises(TypeError, match=msg): |
| 1065 | + _check(float_frame_fill0) |
| 1066 | + with pytest.raises(TypeError, match=msg): |
| 1067 | + _check(float_frame_fill2) |
1051 | 1068 |
|
1052 | 1069 | def test_transpose(self, float_frame, float_frame_int_kind,
|
1053 | 1070 | float_frame_dense,
|
|
0 commit comments