Skip to content

TST: Added tests for ABC classes #38588

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 2 commits into from
Dec 21, 2020
Merged
Changes from 1 commit
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
71 changes: 71 additions & 0 deletions pandas/tests/dtypes/test_generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import itertools as it
from warnings import catch_warnings

import numpy as np
import pytest

from pandas.core.dtypes import generic as gt

Expand Down Expand Up @@ -43,6 +45,75 @@ def test_abc_types(self):
assert isinstance(self.timedelta_array, gt.ABCTimedeltaArray)
assert not isinstance(self.timedelta_index, gt.ABCTimedeltaArray)

abc_pairs = [
("ABCInt64Index", pd.Int64Index([1, 2, 3])),
("ABCUInt64Index", pd.UInt64Index([1, 2, 3])),
("ABCFloat64Index", pd.Float64Index([1, 2, 3])),
("ABCMultiIndex", multi_index),
("ABCDatetimeIndex", datetime_index),
("ABCRangeIndex", pd.RangeIndex(3)),
("ABCTimedeltaIndex", timedelta_index),
("ABCIntervalIndex", pd.interval_range(start=0, end=3)),
("ABCPeriodArray", pd.arrays.PeriodArray([2000, 2001, 2002], freq="D")),
("ABCPandasArray", pd.arrays.PandasArray(np.array([0, 1, 2]))),
("ABCPeriodIndex", period_index),
("ABCCategoricalIndex", categorical_df.index),
("ABCSeries", pd.Series([1, 2, 3])),
("ABCDataFrame", df),
("ABCCategorical", categorical),
("ABCDatetimeArray", datetime_array),
("ABCTimedeltaArray", timedelta_array),
]

@pytest.mark.parametrize(
"abctype1, abctype2, inst",
[
(abctype1, abctype2, inst)
for (abctype1, inst), (abctype2, _) in it.product(abc_pairs, repeat=2)
],
)
def test_abc_pairs(self, abctype1, abctype2, inst):
if abctype1 == abctype2:
assert isinstance(inst, getattr(gt, abctype2))
else:
assert not isinstance(inst, getattr(gt, abctype2))

abc_subclasses = {
"ABCIndex": [
abctype
for abctype, _ in abc_pairs
if "Index" in abctype and abctype != "ABCIndex"
],
"ABCNDFrame": ["ABCSeries", "ABCDataFrame"],
"ABCExtensionArray": [
"ABCCategorical",
"ABCDatetimeArray",
"ABCPeriodArray",
"ABCTimedeltaArray",
],
}

@pytest.mark.parametrize(
"parent, subs, abctype, inst",
[
(parent, subs, abctype, inst)
for (parent, subs), (abctype, inst) in it.product(
abc_subclasses.items(), abc_pairs
Copy link
Member

Choose a reason for hiding this comment

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

we cant do this with two separate @pytest.mark.parametrize lines?

)
],
)
def test_abc_hierarchy(self, parent, subs, abctype, inst):
if abctype in subs:
assert isinstance(inst, getattr(gt, parent))
else:
assert not isinstance(inst, getattr(gt, parent))

@pytest.mark.parametrize("abctype", [e for e in gt.__dict__ if e.startswith("ABC")])
def test_abc_coverage(self, abctype):
assert (
abctype in (e for e, _ in self.abc_pairs) or abctype in self.abc_subclasses
)


def test_setattr_warnings():
# GH7175 - GOTCHA: You can't use dot notation to add a column...
Expand Down