|
1 | 1 | from warnings import catch_warnings
|
2 | 2 |
|
3 | 3 | import numpy as np
|
| 4 | +import pytest |
4 | 5 |
|
5 | 6 | from pandas.core.dtypes import generic as gt
|
6 | 7 |
|
@@ -43,6 +44,66 @@ def test_abc_types(self):
|
43 | 44 | assert isinstance(self.timedelta_array, gt.ABCTimedeltaArray)
|
44 | 45 | assert not isinstance(self.timedelta_index, gt.ABCTimedeltaArray)
|
45 | 46 |
|
| 47 | + abc_pairs = [ |
| 48 | + ("ABCInt64Index", pd.Int64Index([1, 2, 3])), |
| 49 | + ("ABCUInt64Index", pd.UInt64Index([1, 2, 3])), |
| 50 | + ("ABCFloat64Index", pd.Float64Index([1, 2, 3])), |
| 51 | + ("ABCMultiIndex", multi_index), |
| 52 | + ("ABCDatetimeIndex", datetime_index), |
| 53 | + ("ABCRangeIndex", pd.RangeIndex(3)), |
| 54 | + ("ABCTimedeltaIndex", timedelta_index), |
| 55 | + ("ABCIntervalIndex", pd.interval_range(start=0, end=3)), |
| 56 | + ("ABCPeriodArray", pd.arrays.PeriodArray([2000, 2001, 2002], freq="D")), |
| 57 | + ("ABCPandasArray", pd.arrays.PandasArray(np.array([0, 1, 2]))), |
| 58 | + ("ABCPeriodIndex", period_index), |
| 59 | + ("ABCCategoricalIndex", categorical_df.index), |
| 60 | + ("ABCSeries", pd.Series([1, 2, 3])), |
| 61 | + ("ABCDataFrame", df), |
| 62 | + ("ABCCategorical", categorical), |
| 63 | + ("ABCDatetimeArray", datetime_array), |
| 64 | + ("ABCTimedeltaArray", timedelta_array), |
| 65 | + ] |
| 66 | + |
| 67 | + @pytest.mark.parametrize("abctype1, inst", abc_pairs) |
| 68 | + @pytest.mark.parametrize("abctype2, _", abc_pairs) |
| 69 | + def test_abc_pairs(self, abctype1, abctype2, inst, _): |
| 70 | + # GH 38588 |
| 71 | + if abctype1 == abctype2: |
| 72 | + assert isinstance(inst, getattr(gt, abctype2)) |
| 73 | + else: |
| 74 | + assert not isinstance(inst, getattr(gt, abctype2)) |
| 75 | + |
| 76 | + abc_subclasses = { |
| 77 | + "ABCIndex": [ |
| 78 | + abctype |
| 79 | + for abctype, _ in abc_pairs |
| 80 | + if "Index" in abctype and abctype != "ABCIndex" |
| 81 | + ], |
| 82 | + "ABCNDFrame": ["ABCSeries", "ABCDataFrame"], |
| 83 | + "ABCExtensionArray": [ |
| 84 | + "ABCCategorical", |
| 85 | + "ABCDatetimeArray", |
| 86 | + "ABCPeriodArray", |
| 87 | + "ABCTimedeltaArray", |
| 88 | + ], |
| 89 | + } |
| 90 | + |
| 91 | + @pytest.mark.parametrize("parent, subs", abc_subclasses.items()) |
| 92 | + @pytest.mark.parametrize("abctype, inst", abc_pairs) |
| 93 | + def test_abc_hierarchy(self, parent, subs, abctype, inst): |
| 94 | + # GH 38588 |
| 95 | + if abctype in subs: |
| 96 | + assert isinstance(inst, getattr(gt, parent)) |
| 97 | + else: |
| 98 | + assert not isinstance(inst, getattr(gt, parent)) |
| 99 | + |
| 100 | + @pytest.mark.parametrize("abctype", [e for e in gt.__dict__ if e.startswith("ABC")]) |
| 101 | + def test_abc_coverage(self, abctype): |
| 102 | + # GH 38588 |
| 103 | + assert ( |
| 104 | + abctype in (e for e, _ in self.abc_pairs) or abctype in self.abc_subclasses |
| 105 | + ) |
| 106 | + |
46 | 107 |
|
47 | 108 | def test_setattr_warnings():
|
48 | 109 | # GH7175 - GOTCHA: You can't use dot notation to add a column...
|
|
0 commit comments