-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: .dot tests #33214
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
REF: .dot tests #33214
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54c91de
REF: .dot tests
jbrockmendel f3963a3
Update pandas/tests/generic/methods/test_dot.py
jbrockmendel 01ba035
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 9340dd5
Merge branch 'tst-matmul' of github.com:jbrockmendel/pandas into tst-…
jbrockmendel 565c25c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 08ba91b
remove unused import
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, Series | ||
import pandas._testing as tm | ||
|
||
|
||
class DotSharedTests: | ||
@pytest.fixture | ||
def obj(self): | ||
raise NotImplementedError | ||
|
||
@pytest.fixture | ||
def other(self) -> DataFrame: | ||
""" | ||
other is a DataFrame that is indexed so that obj.dot(other) is valid | ||
""" | ||
raise NotImplementedError | ||
|
||
@pytest.fixture | ||
def expected(self, obj, other) -> DataFrame: | ||
""" | ||
The expected result of obj.dot(other) | ||
""" | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def reduced_dim_assert(cls, result, expected): | ||
""" | ||
Assertion about results with 1 fewer dimension that self.obj | ||
""" | ||
raise NotImplementedError | ||
|
||
def test_dot_equiv_values_dot(self, obj, other, expected): | ||
# `expected` is constructed from obj.values.dot(other.values) | ||
result = obj.dot(other) | ||
tm.assert_equal(result, expected) | ||
|
||
def test_dot_2d_ndarray(self, obj, other, expected): | ||
# Check ndarray argument; in this case we get matching values, | ||
# but index/columns may not match | ||
result = obj.dot(other.values) | ||
assert np.all(result == expected.values) | ||
|
||
def test_dot_1d_ndarray(self, obj, expected): | ||
# can pass correct-length array | ||
row = obj.iloc[0] if obj.ndim == 2 else obj | ||
|
||
result = obj.dot(row.values) | ||
expected = obj.dot(row) | ||
self.reduced_dim_assert(result, expected) | ||
|
||
def test_dot_series(self, obj, other, expected): | ||
# Check series argument | ||
result = obj.dot(other["1"]) | ||
self.reduced_dim_assert(result, expected["1"]) | ||
|
||
def test_dot_series_alignment(self, obj, other, expected): | ||
result = obj.dot(other.iloc[::-1]["1"]) | ||
self.reduced_dim_assert(result, expected["1"]) | ||
|
||
def test_dot_aligns(self, obj, other, expected): | ||
# Check index alignment | ||
other2 = other.iloc[::-1] | ||
result = obj.dot(other2) | ||
tm.assert_equal(result, expected) | ||
|
||
def test_dot_shape_mismatch(self, obj): | ||
msg = "Dot product shape mismatch" | ||
# exception raised is of type Exception | ||
with pytest.raises(Exception, match=msg): | ||
obj.dot(obj.values[:3]) | ||
|
||
def test_dot_misaligned(self, obj, other): | ||
msg = "matrices are not aligned" | ||
with pytest.raises(ValueError, match=msg): | ||
obj.dot(other.T) | ||
|
||
|
||
class TestSeriesDot(DotSharedTests): | ||
@pytest.fixture | ||
def obj(self): | ||
return Series(np.random.randn(4), index=["p", "q", "r", "s"]) | ||
|
||
@pytest.fixture | ||
def other(self): | ||
return DataFrame( | ||
np.random.randn(3, 4), index=["1", "2", "3"], columns=["p", "q", "r", "s"] | ||
).T | ||
|
||
@pytest.fixture | ||
def expected(self, obj, other): | ||
return Series(np.dot(obj.values, other.values), index=other.columns) | ||
|
||
@classmethod | ||
def reduced_dim_assert(cls, result, expected): | ||
""" | ||
Assertion about results with 1 fewer dimension that self.obj | ||
""" | ||
tm.assert_almost_equal(result, expected) | ||
|
||
|
||
class TestDataFrameDot(DotSharedTests): | ||
@pytest.fixture | ||
def obj(self): | ||
return DataFrame( | ||
np.random.randn(3, 4), index=["a", "b", "c"], columns=["p", "q", "r", "s"] | ||
) | ||
|
||
@pytest.fixture | ||
def other(self): | ||
return DataFrame( | ||
np.random.randn(4, 2), index=["p", "q", "r", "s"], columns=["1", "2"] | ||
) | ||
|
||
@pytest.fixture | ||
def expected(self, obj, other): | ||
return DataFrame( | ||
np.dot(obj.values, other.values), index=obj.index, columns=other.columns | ||
) | ||
|
||
@classmethod | ||
def reduced_dim_assert(cls, result, expected): | ||
""" | ||
Assertion about results with 1 fewer dimension that self.obj | ||
""" | ||
tm.assert_series_equal(result, expected, check_names=False) | ||
assert result.name is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make these module level fixtures, having in a class is an anti-patter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even better is to use the globally defined fixtures (but if hard nbd)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbrockmendel do we need these class level fixtures like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for some value of "need". Simon and i do not have a consensus on this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, the reason i question this is that we do not do this anywhere else (except for a PR you just did), and we DO this also in the resample tests. Since we aren't intentionally doing this globally I want to be sure this pattern is a valid / reasonable usecase that simply doesn't fit into our other patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use it a bunch in tests.indexes