Skip to content

TST: More pytest idioms in tests/generic #45086

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 4 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
assert_interval_array_equal,
assert_is_sorted,
assert_is_valid_plot_return_object,
assert_metadata_equivalent,
assert_numpy_array_equal,
assert_period_array_equal,
assert_series_equal,
Expand Down
12 changes: 12 additions & 0 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,3 +1470,15 @@ def assert_indexing_slices_equivalent(ser: Series, l_slc: slice, i_slc: slice):
if not ser.index.is_integer():
# For integer indices, .loc and plain getitem are position-based.
assert_series_equal(ser[l_slc], expected)


def assert_metadata_equivalent(left, right):
Copy link
Contributor

Choose a reason for hiding this comment

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

ideally type these (followup ok)

Copy link
Contributor

Choose a reason for hiding this comment

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

these have to be Series/DataFrame?

Copy link
Contributor

Choose a reason for hiding this comment

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

also see if you can grep if we have any more _metadata checkers

"""
Check that ._metadata attributes are equivalent.
"""
for attr in left._metadata:
val = getattr(left, attr, None)
if right is None:
assert val is None
else:
assert val == getattr(right, attr, None)
12 changes: 4 additions & 8 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
date_range,
)
import pandas._testing as tm
from pandas.tests.generic.test_generic import Generic


class TestDataFrame(Generic):
_typ = DataFrame
_comparator = lambda self, x, y: tm.assert_frame_equal(x, y)

class TestDataFrame:
@pytest.mark.parametrize("func", ["_set_axis_name", "rename_axis"])
def test_set_axis_name(self, func):
df = DataFrame([[1, 2], [3, 4]])
Expand Down Expand Up @@ -76,7 +72,7 @@ def test_metadata_propagation_indiv_groupby(self):
}
)
result = df.groupby("A").sum()
self.check_metadata(df, result)
tm.assert_metadata_equivalent(df, result)

def test_metadata_propagation_indiv_resample(self):
# resample
Expand All @@ -85,7 +81,7 @@ def test_metadata_propagation_indiv_resample(self):
index=date_range("20130101", periods=1000, freq="s"),
)
result = df.resample("1T")
self.check_metadata(df, result)
tm.assert_metadata_equivalent(df, result)

def test_metadata_propagation_indiv(self, monkeypatch):
# merging with override
Expand Down Expand Up @@ -148,7 +144,7 @@ def test_deepcopy_empty(self):
empty_frame = DataFrame(data=[], index=[], columns=["A"])
empty_frame_copy = deepcopy(empty_frame)

self._compare(empty_frame_copy, empty_frame)
tm.assert_frame_equal(empty_frame_copy, empty_frame)


# formerly in Generic but only test DataFrame
Expand Down
Loading