From b88e553278f78b3a05912be381a14d61c6dfe718 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 1 Feb 2020 19:45:58 -0800 Subject: [PATCH 1/4] implement tests/indexes/objects/ --- pandas/tests/indexes/objects/__init__.py | 0 pandas/tests/indexes/objects/test_constructors.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 pandas/tests/indexes/objects/__init__.py create mode 100644 pandas/tests/indexes/objects/test_constructors.py diff --git a/pandas/tests/indexes/objects/__init__.py b/pandas/tests/indexes/objects/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/tests/indexes/objects/test_constructors.py b/pandas/tests/indexes/objects/test_constructors.py new file mode 100644 index 0000000000000..e69de29bb2d1d From 9e15ae2b1377067e7c92f8bcadce656a07938306 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 1 Feb 2020 19:57:36 -0800 Subject: [PATCH 2/4] implement test_setops --- pandas/tests/indexes/objects/test_setops.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pandas/tests/indexes/objects/test_setops.py diff --git a/pandas/tests/indexes/objects/test_setops.py b/pandas/tests/indexes/objects/test_setops.py new file mode 100644 index 0000000000000..2afad93c271b6 --- /dev/null +++ b/pandas/tests/indexes/objects/test_setops.py @@ -0,0 +1,4 @@ + + +class TestIndexSetOps: + pass From 370bab43930ab3e30c11f706f9c1523d0390702d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 1 Feb 2020 20:04:40 -0800 Subject: [PATCH 3/4] Move some tests to object tests --- .../indexes/objects/test_constructors.py | 36 +++++++ pandas/tests/indexes/objects/test_setops.py | 72 +++++++++++++- pandas/tests/indexes/test_base.py | 95 ------------------- 3 files changed, 107 insertions(+), 96 deletions(-) diff --git a/pandas/tests/indexes/objects/test_constructors.py b/pandas/tests/indexes/objects/test_constructors.py index e69de29bb2d1d..9e6a8f34c135d 100644 --- a/pandas/tests/indexes/objects/test_constructors.py +++ b/pandas/tests/indexes/objects/test_constructors.py @@ -0,0 +1,36 @@ +import pytest + +from pandas import Index, MultiIndex + + +class TestIndexConstructor: + # Tests for the Index constructor, specifically for cases that do + # not return a subclass + + def test_constructor_corner(self): + # corner case + msg = ( + r"Index\(\.\.\.\) must be called with a collection of some " + "kind, 0 was passed" + ) + with pytest.raises(TypeError, match=msg): + Index(0) + + @pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]]) + def test_construction_list_mixed_tuples(self, index_vals): + # see gh-10697: if we are constructing from a mixed list of tuples, + # make sure that we are independent of the sorting order. + index = Index(index_vals) + assert isinstance(index, Index) + assert not isinstance(index, MultiIndex) + + def test_constructor_wrong_kwargs(self): + # GH #19348 + with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"): + Index([], foo="bar") + + @pytest.mark.xfail(reason="see GH#21311: Index doesn't enforce dtype argument") + def test_constructor_cast(self): + msg = "could not convert string to float" + with pytest.raises(ValueError, match=msg): + Index(["a", "b", "c"], dtype=float) diff --git a/pandas/tests/indexes/objects/test_setops.py b/pandas/tests/indexes/objects/test_setops.py index 2afad93c271b6..e7d5e21d0ba47 100644 --- a/pandas/tests/indexes/objects/test_setops.py +++ b/pandas/tests/indexes/objects/test_setops.py @@ -1,4 +1,74 @@ +import numpy as np +import pytest + +from pandas import Index, Series +import pandas._testing as tm +from pandas.core.algorithms import safe_sort class TestIndexSetOps: - pass + def test_union_base(self): + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[3:] + second = index[:5] + + result = first.union(second) + + expected = Index([0, 1, 2, "a", "b", "c"]) + tm.assert_index_equal(result, expected) + + @pytest.mark.parametrize("klass", [np.array, Series, list]) + def test_union_different_type_base(self, klass): + # GH 10149 + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[3:] + second = index[:5] + + result = first.union(klass(second.values)) + + assert tm.equalContents(result, index) + + @pytest.mark.parametrize("sort", [None, False]) + def test_intersection_base(self, sort): + # (same results for py2 and py3 but sortedness not tested elsewhere) + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[:5] + second = index[:3] + + expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1]) + result = first.intersection(second, sort=sort) + tm.assert_index_equal(result, expected) + + @pytest.mark.parametrize("klass", [np.array, Series, list]) + @pytest.mark.parametrize("sort", [None, False]) + def test_intersection_different_type_base(self, klass, sort): + # GH 10149 + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[:5] + second = index[:3] + + result = first.intersection(klass(second.values), sort=sort) + assert tm.equalContents(result, second) + + @pytest.mark.parametrize("sort", [None, False]) + def test_difference_base(self, sort): + # (same results for py2 and py3 but sortedness not tested elsewhere) + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[:4] + second = index[3:] + + result = first.difference(second, sort) + expected = Index([0, "a", 1]) + if sort is None: + expected = Index(safe_sort(expected)) + tm.assert_index_equal(result, expected) + + def test_symmetric_difference(self): + # (same results for py2 and py3 but sortedness not tested elsewhere) + index = Index([0, "a", 1, "b", 2, "c"]) + first = index[:4] + second = index[3:] + + result = first.symmetric_difference(second) + expected = Index([0, 1, 2, "a", "c"]) + tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index e72963de09ab4..7ee555b4245ff 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -34,7 +34,6 @@ period_range, ) import pandas._testing as tm -from pandas.core.algorithms import safe_sort from pandas.core.indexes.api import ( Index, MultiIndex, @@ -108,23 +107,6 @@ def test_constructor_copy(self, index): # arr = np.array(5.) # pytest.raises(Exception, arr.view, Index) - def test_constructor_corner(self): - # corner case - msg = ( - r"Index\(\.\.\.\) must be called with a collection of some " - "kind, 0 was passed" - ) - with pytest.raises(TypeError, match=msg): - Index(0) - - @pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]]) - def test_construction_list_mixed_tuples(self, index_vals): - # see gh-10697: if we are constructing from a mixed list of tuples, - # make sure that we are independent of the sorting order. - index = Index(index_vals) - assert isinstance(index, Index) - assert not isinstance(index, MultiIndex) - @pytest.mark.parametrize("na_value", [None, np.nan]) @pytest.mark.parametrize("vtype", [list, tuple, iter]) def test_construction_list_tuples_nan(self, na_value, vtype): @@ -359,11 +341,6 @@ def test_constructor_simple_new(self, vals, dtype): result = index._simple_new(index.values, dtype) tm.assert_index_equal(result, index) - def test_constructor_wrong_kwargs(self): - # GH #19348 - with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"): - Index([], foo="bar") - @pytest.mark.parametrize( "vals", [ @@ -554,12 +531,6 @@ def test_constructor_overflow_int64(self): with pytest.raises(OverflowError, match=msg): Index([np.iinfo(np.uint64).max - 1], dtype="int64") - @pytest.mark.xfail(reason="see GH#21311: Index doesn't enforce dtype argument") - def test_constructor_cast(self): - msg = "could not convert string to float" - with pytest.raises(ValueError, match=msg): - Index(["a", "b", "c"], dtype=float) - @pytest.mark.parametrize( "index", [ @@ -2502,78 +2473,12 @@ def test_copy_name2(self): assert index3.name == "NewName" assert index3.names == ["NewName"] - def test_union_base(self): - index = self.create_index() - first = index[3:] - second = index[:5] - - result = first.union(second) - - expected = Index([0, 1, 2, "a", "b", "c"]) - tm.assert_index_equal(result, expected) - - @pytest.mark.parametrize("klass", [np.array, Series, list]) - def test_union_different_type_base(self, klass): - # GH 10149 - index = self.create_index() - first = index[3:] - second = index[:5] - - result = first.union(klass(second.values)) - - assert tm.equalContents(result, index) - def test_unique_na(self): idx = pd.Index([2, np.nan, 2, 1], name="my_index") expected = pd.Index([2, np.nan, 1], name="my_index") result = idx.unique() tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("sort", [None, False]) - def test_intersection_base(self, sort): - # (same results for py2 and py3 but sortedness not tested elsewhere) - index = self.create_index() - first = index[:5] - second = index[:3] - - expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1]) - result = first.intersection(second, sort=sort) - tm.assert_index_equal(result, expected) - - @pytest.mark.parametrize("klass", [np.array, Series, list]) - @pytest.mark.parametrize("sort", [None, False]) - def test_intersection_different_type_base(self, klass, sort): - # GH 10149 - index = self.create_index() - first = index[:5] - second = index[:3] - - result = first.intersection(klass(second.values), sort=sort) - assert tm.equalContents(result, second) - - @pytest.mark.parametrize("sort", [None, False]) - def test_difference_base(self, sort): - # (same results for py2 and py3 but sortedness not tested elsewhere) - index = self.create_index() - first = index[:4] - second = index[3:] - - result = first.difference(second, sort) - expected = Index([0, "a", 1]) - if sort is None: - expected = Index(safe_sort(expected)) - tm.assert_index_equal(result, expected) - - def test_symmetric_difference(self): - # (same results for py2 and py3 but sortedness not tested elsewhere) - index = self.create_index() - first = index[:4] - second = index[3:] - - result = first.symmetric_difference(second) - expected = Index([0, 1, 2, "a", "c"]) - tm.assert_index_equal(result, expected) - def test_logical_compat(self): index = self.create_index() assert index.all() == index.values.all() From ba8fa6503b3e74dc86531f7230a0bfb323e5b9bc Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 4 Feb 2020 09:16:53 -0800 Subject: [PATCH 4/4] rename objects/ -> base_class/ --- pandas/tests/indexes/{objects => base_class}/__init__.py | 0 pandas/tests/indexes/{objects => base_class}/test_constructors.py | 0 pandas/tests/indexes/{objects => base_class}/test_setops.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename pandas/tests/indexes/{objects => base_class}/__init__.py (100%) rename pandas/tests/indexes/{objects => base_class}/test_constructors.py (100%) rename pandas/tests/indexes/{objects => base_class}/test_setops.py (100%) diff --git a/pandas/tests/indexes/objects/__init__.py b/pandas/tests/indexes/base_class/__init__.py similarity index 100% rename from pandas/tests/indexes/objects/__init__.py rename to pandas/tests/indexes/base_class/__init__.py diff --git a/pandas/tests/indexes/objects/test_constructors.py b/pandas/tests/indexes/base_class/test_constructors.py similarity index 100% rename from pandas/tests/indexes/objects/test_constructors.py rename to pandas/tests/indexes/base_class/test_constructors.py diff --git a/pandas/tests/indexes/objects/test_setops.py b/pandas/tests/indexes/base_class/test_setops.py similarity index 100% rename from pandas/tests/indexes/objects/test_setops.py rename to pandas/tests/indexes/base_class/test_setops.py