|
3 | 3 | import pytest
|
4 | 4 |
|
5 | 5 | from datetime import datetime, timedelta
|
6 |
| - |
| 6 | +from decimal import Decimal |
7 | 7 | from collections import defaultdict
|
8 | 8 |
|
9 | 9 | import pandas.util.testing as tm
|
@@ -864,13 +864,47 @@ def test_add(self):
|
864 | 864 | expected = Index(['1a', '1b', '1c'])
|
865 | 865 | tm.assert_index_equal('1' + index, expected)
|
866 | 866 |
|
867 |
| - def test_sub(self): |
| 867 | + def test_sub_fail(self): |
868 | 868 | index = self.strIndex
|
869 | 869 | pytest.raises(TypeError, lambda: index - 'a')
|
870 | 870 | pytest.raises(TypeError, lambda: index - index)
|
871 | 871 | pytest.raises(TypeError, lambda: index - index.tolist())
|
872 | 872 | pytest.raises(TypeError, lambda: index.tolist() - index)
|
873 | 873 |
|
| 874 | + def test_sub_object(self): |
| 875 | + # GH#19369 |
| 876 | + index = pd.Index([Decimal(1), Decimal(2)]) |
| 877 | + expected = pd.Index([Decimal(0), Decimal(1)]) |
| 878 | + |
| 879 | + result = index - Decimal(1) |
| 880 | + tm.assert_index_equal(result, expected) |
| 881 | + |
| 882 | + result = index - pd.Index([Decimal(1), Decimal(1)]) |
| 883 | + tm.assert_index_equal(result, expected) |
| 884 | + |
| 885 | + with pytest.raises(TypeError): |
| 886 | + index - 'foo' |
| 887 | + |
| 888 | + with pytest.raises(TypeError): |
| 889 | + index - np.array([2, 'foo']) |
| 890 | + |
| 891 | + def test_rsub_object(self): |
| 892 | + # GH#19369 |
| 893 | + index = pd.Index([Decimal(1), Decimal(2)]) |
| 894 | + expected = pd.Index([Decimal(1), Decimal(0)]) |
| 895 | + |
| 896 | + result = Decimal(2) - index |
| 897 | + tm.assert_index_equal(result, expected) |
| 898 | + |
| 899 | + result = np.array([Decimal(2), Decimal(2)]) - index |
| 900 | + tm.assert_index_equal(result, expected) |
| 901 | + |
| 902 | + with pytest.raises(TypeError): |
| 903 | + 'foo' - index |
| 904 | + |
| 905 | + with pytest.raises(TypeError): |
| 906 | + np.array([True, pd.Timestamp.now()]) - index |
| 907 | + |
874 | 908 | def test_map_identity_mapping(self):
|
875 | 909 | # GH 12766
|
876 | 910 | # TODO: replace with fixture
|
|
0 commit comments