|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas import DataFrame, Series |
| 5 | +import pandas._testing as tm |
| 6 | + |
| 7 | + |
| 8 | +class DotSharedTests: |
| 9 | + @pytest.fixture |
| 10 | + def obj(self): |
| 11 | + raise NotImplementedError |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def other(self) -> DataFrame: |
| 15 | + """ |
| 16 | + other is a DataFrame that is indexed so that obj.dot(other) is valid |
| 17 | + """ |
| 18 | + raise NotImplementedError |
| 19 | + |
| 20 | + @pytest.fixture |
| 21 | + def expected(self, obj, other) -> DataFrame: |
| 22 | + """ |
| 23 | + The expected result of obj.dot(other) |
| 24 | + """ |
| 25 | + raise NotImplementedError |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def reduced_dim_assert(cls, result, expected): |
| 29 | + """ |
| 30 | + Assertion about results with 1 fewer dimension that self.obj |
| 31 | + """ |
| 32 | + raise NotImplementedError |
| 33 | + |
| 34 | + def test_dot_equiv_values_dot(self, obj, other, expected): |
| 35 | + # `expected` is constructed from obj.values.dot(other.values) |
| 36 | + result = obj.dot(other) |
| 37 | + tm.assert_equal(result, expected) |
| 38 | + |
| 39 | + def test_dot_2d_ndarray(self, obj, other, expected): |
| 40 | + # Check ndarray argument; in this case we get matching values, |
| 41 | + # but index/columns may not match |
| 42 | + result = obj.dot(other.values) |
| 43 | + assert np.all(result == expected.values) |
| 44 | + |
| 45 | + def test_dot_1d_ndarray(self, obj, expected): |
| 46 | + # can pass correct-length array |
| 47 | + row = obj.iloc[0] if obj.ndim == 2 else obj |
| 48 | + |
| 49 | + result = obj.dot(row.values) |
| 50 | + expected = obj.dot(row) |
| 51 | + self.reduced_dim_assert(result, expected) |
| 52 | + |
| 53 | + def test_dot_series(self, obj, other, expected): |
| 54 | + # Check series argument |
| 55 | + result = obj.dot(other["1"]) |
| 56 | + self.reduced_dim_assert(result, expected["1"]) |
| 57 | + |
| 58 | + def test_dot_series_alignment(self, obj, other, expected): |
| 59 | + result = obj.dot(other.iloc[::-1]["1"]) |
| 60 | + self.reduced_dim_assert(result, expected["1"]) |
| 61 | + |
| 62 | + def test_dot_aligns(self, obj, other, expected): |
| 63 | + # Check index alignment |
| 64 | + other2 = other.iloc[::-1] |
| 65 | + result = obj.dot(other2) |
| 66 | + tm.assert_equal(result, expected) |
| 67 | + |
| 68 | + def test_dot_shape_mismatch(self, obj): |
| 69 | + msg = "Dot product shape mismatch" |
| 70 | + # exception raised is of type Exception |
| 71 | + with pytest.raises(Exception, match=msg): |
| 72 | + obj.dot(obj.values[:3]) |
| 73 | + |
| 74 | + def test_dot_misaligned(self, obj, other): |
| 75 | + msg = "matrices are not aligned" |
| 76 | + with pytest.raises(ValueError, match=msg): |
| 77 | + obj.dot(other.T) |
| 78 | + |
| 79 | + |
| 80 | +class TestSeriesDot(DotSharedTests): |
| 81 | + @pytest.fixture |
| 82 | + def obj(self): |
| 83 | + return Series(np.random.randn(4), index=["p", "q", "r", "s"]) |
| 84 | + |
| 85 | + @pytest.fixture |
| 86 | + def other(self): |
| 87 | + return DataFrame( |
| 88 | + np.random.randn(3, 4), index=["1", "2", "3"], columns=["p", "q", "r", "s"] |
| 89 | + ).T |
| 90 | + |
| 91 | + @pytest.fixture |
| 92 | + def expected(self, obj, other): |
| 93 | + return Series(np.dot(obj.values, other.values), index=other.columns) |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def reduced_dim_assert(cls, result, expected): |
| 97 | + """ |
| 98 | + Assertion about results with 1 fewer dimension that self.obj |
| 99 | + """ |
| 100 | + tm.assert_almost_equal(result, expected) |
| 101 | + |
| 102 | + |
| 103 | +class TestDataFrameDot(DotSharedTests): |
| 104 | + @pytest.fixture |
| 105 | + def obj(self): |
| 106 | + return DataFrame( |
| 107 | + np.random.randn(3, 4), index=["a", "b", "c"], columns=["p", "q", "r", "s"] |
| 108 | + ) |
| 109 | + |
| 110 | + @pytest.fixture |
| 111 | + def other(self): |
| 112 | + return DataFrame( |
| 113 | + np.random.randn(4, 2), index=["p", "q", "r", "s"], columns=["1", "2"] |
| 114 | + ) |
| 115 | + |
| 116 | + @pytest.fixture |
| 117 | + def expected(self, obj, other): |
| 118 | + return DataFrame( |
| 119 | + np.dot(obj.values, other.values), index=obj.index, columns=other.columns |
| 120 | + ) |
| 121 | + |
| 122 | + @classmethod |
| 123 | + def reduced_dim_assert(cls, result, expected): |
| 124 | + """ |
| 125 | + Assertion about results with 1 fewer dimension that self.obj |
| 126 | + """ |
| 127 | + tm.assert_series_equal(result, expected, check_names=False) |
| 128 | + assert result.name is None |
0 commit comments